如何在mvc4中上传图片? [英] how to upload image in mvc4?
本文介绍了如何在mvc4中上传图片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码,但它不起作用
here is my code but its not work
[HttpPost]
public ActionResult AddUser(HttpPostedFileBase file)
{
if (file.ContentLength > 0)
{
string name = Path.GetFileName(file.FileName);
}
return View();
}
这里是html代码
here is html code
<input type="file" id="image"/>
推荐答案
检查是否通过创建断点,HttpPostedFileBase对象不为null,如果有值,则使用类似
check if HttpPostedFileBase object is not null by making break point, and if it have value use code like
var path = "";
var fileName = "";
string logo = "";
fileName = Path.GetFileName(file.FileName);
string Date = DateTime.Now.ToShortDateString();
string Millesec = DateTime.Now.Millisecond.ToString();
string Add = Date.Replace("/", "") + Millesec;
logo = Add + fileName;
path = Path.Combine(Server.MapPath("~/Images/profile/"), logo);
file.SaveAs(path);
检查以下网址问题是否相同你的
http://stackoverflow.com/questions/16255882/how-to-upload-image-display-image-in-asp-net-mvc-4
希望这有帮助。
Hi,
Check the below url the problem is same as that of yours
http://stackoverflow.com/questions/16255882/how-to-upload-image-display-image-in-asp-net-mvc-4
Hope this helps.
这篇关于如何在mvc4中上传图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文