使用asp.net mvc上传图片 [英] Upload Image by using asp.net mvc
本文介绍了使用asp.net mvc上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
亲爱的,
我想使用asp.net mvc 3上传图片。我使用razor(cshtml)。我未能完成这项工作。
我可以通过asp.net webform上传图片。但需要使用mvc。
请尽快给我全程帮助。
谢谢,
Emon Mahmud
Dear all,
I want to upload an image by using asp.net mvc 3. I use razor(cshtml).I have failed to finish this work.
I am able to upload image by asp.net webform. but need to do using mvc.
Please help full process to me soon.
Thanks,
Emon Mahmud
推荐答案
亲爱的Mahmud,
您可以使用HttpPostedFileBase。 InputStream.Read()方法将上传流读取到缓冲区,然后将其保存到数据库字段中。例如:
Dear Mahmud,
You can use HttpPostedFileBase.InputStream.Read() method read the upload stream to a buffer, and then save it to the database field. For example:
[HttpPost]
public ActionResult Create(string fileTitle)
{
try
{
HttpPostedFileBase file = Request.Files[0];
byte[] imageSize = new byte[file.ContentLength];
file.InputStream.Read(imageSize, 0, (int)file.ContentLength);
Image image = new Image()
{
Name = file.FileName.Split('\\').Last(),
Size = file.ContentLength,
Title = fileTitle,
ID = 1,
Image1 = imageSize
};
db.Images.AddObject(image);
db.SaveChanges();
return RedirectToAction("Detail");
}
catch(Exception e)
{
ModelState.AddModelError("uploadError", e);
}
return View();
}
查看
View
@using (Html.BeginForm("Create", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))
{
<fieldset>
<legend>Upload Image</legend>
@Html.Label("Title")
@Html.Editor("fileTitle")<br />
Upload File: <input type="file" name="test" />
<p>
<input type="submit" value="Create" />
</p>
</fieldset>
}
我认为这会对你有所帮助
谢谢
I think this will helpful to you
Thanks
您好,请查看此链接
http://www.dotnetdreamer.net/upload-imagedisplay-image-in-aspnet-mvc [ ^ ]
Hi check this link out
http://www.dotnetdreamer.net/upload-imagedisplay-image-in-aspnet-mvc[^]
这篇关于使用asp.net mvc上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文