我想上傳幾個數據庫圖像到SQL Server 2008R2上。我在C#中使用ASP.NET MVC 3。發生什麼事是我得到的圖像顯示,但問題是,第二個圖像顯示爲兩次。所以它是重複的。我不確定爲什麼沒有顯示第一張圖片。無法上傳多個數據庫圖像與asp.net mvc
我SubProductCategory4表有以下幾列(爲簡單起見)...
列名稱:圖像1和圖像2具有數據類型VARBINARY(MAX),另一列名稱:ImageMimeType有數據類型爲varchar(50)。
我的控制器,具有針對創建方法如下代碼...
[HttpPost]
public ActionResult Create([Bind(Exclude = "SubProductCategoryFourID")] SubProductCategory4 Createsubcat4, IEnumerable<HttpPostedFileBase> files, FormCollection collection)
{
if (ModelState.IsValid)
{
foreach (string inputTagName in Request.Files)
{
if (Request.Files.Count > 0) // tried Files.Count > 1 did
// not solve the problem
{
Createsubcat4.Image1 = (new FileHandler()).uploadedFileToByteArray((HttpPostedFileBase)Request.Files[inputTagName]);
Createsubcat4.Image2 = (new FileHandler()).uploadedFileToByteArray((HttpPostedFileBase)Request.Files[inputTagName]);
// var fileName = Path.GetFileName(inputTagName);
//var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
}
// moved db.AddToSubProductCategory4(Createsubcat4);
// here but did not solve the problem
}
db.AddToSubProductCategory4(Createsubcat4);
db.SaveChanges();
return RedirectToAction("/");
}
//someother code
return View(Createsubcat4);
}
的getImage方法...
public FileResult GetImage(int id)
{
const string alternativePicturePath = @"/Content/question_mark.jpg";
MemoryStream stream;
MemoryStream streaml;
SubProductCategory4 z = db.SubProductCategory4.Where(k => k.SubProductCategoryFourID == id).FirstOrDefault();
if ((z != null && z.Image1 != null) && (z != null && z.Image2 != null))
{
stream = new MemoryStream(z.Image1);
streaml = new MemoryStream(z.Image2);
}
else
{
var path = Server.MapPath(alternativePicturePath);
foreach (byte item in Request.Files)
{
HttpPostedFileBase file = Request.Files[item];
if (file.ContentLength == 0)
{
continue;
}
}
stream = new MemoryStream();
var imagex = new System.Drawing.Bitmap(path);
imagex.Save(stream, System.Drawing.Imaging.ImageFormat.Jpeg);
stream.Seek(0, SeekOrigin.Begin);
/* streaml = new MemoryStream();
var imagey = new System.Drawing.Bitmap(path);
imagey.Save(streaml, System.Drawing.Imaging.ImageFormat.Jpeg);
streaml.Seek(0, SeekOrigin.Begin);*/
}
return new FileStreamResult(stream,"image/jpg");
}
FileHandler.cs
public class FileHandler
{
public byte[] uploadedFileToByteArray(HttpPostedFileBase file)
{
int nFileLen = file.ContentLength;
byte[] result = new byte[nFileLen];
file.InputStream.Read(result, 0, nFileLen);
return result;
}
}
create.cshtml .. 。
@using (Html.BeginForm("Create", "ProductCategoryL4", "GetImage",
FormMethod.Post, new { enctype = "multipart/form-data" }))
//some code then...
<div class="editor-field">
@Html.EditorFor(model => model.Image1)
<input type="file" id="fileUpload1" name="fileUpload1" size="23"/>
@Html.ValidationMessageFor(model => model.Image1)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Image2)
<input type="file" id="fileUpload2" name="fileUpload2" size="23"/>
@Html.ValidationMessageFor(model => model.Image2)
</div>
index.cshtml ...
<img src="@Url.Action("GetImage", "ProductCategoryL4", new { id =
item.SubProductCategoryFourID })" alt="" height="100" width="100" />
</td>
<td>
<img src="@Url.Action("GetImage", "ProductCategoryL4", new { id =
item.SubProductCategoryFourID })" alt="" height="100" width="100" />
</td>
我使用VS2010使用,ASP.NET MVC3在C#與SQL Server 2008R2。提前致謝,但請只回答如果你知道答案。如果有更好的方法,請讓我知道。
@NickLarsen - 我恐怕也沒有解決問題。您提供的代碼,第一張圖像顯示爲兩次。所以它是重複的。 – DiscoDude 2011-04-27 15:05:24
@DiscoDude:所以現在你正在保存圖片,你的'GetImage'動作需要更新。更新我的回覆以幫助您更多。 – 2011-04-27 15:21:58
@DiscoDude:更新的響應 – 2011-04-27 15:34:36