Mvc图像上传无法正常工作
问题描述:
我的价值总是空值,
我尝试过:
控制器,
I got always null value for this,
What I have tried:
Controller,
if (itm.AttachmentModel.AttachmentFile != null)
{
itm.AttachmentModel.AttachmentURL = string.Format("{0}.{1}", Guid.NewGuid().ToString(), itm.AttachmentModel.AttachmentFile.FileName.Split('.')[1]);
if (itm.AttachmentModel.AttachmentFile != null)
{
var path = Path.Combine(Server.MapPath("~/App_Data/Attachments/"), itm.AttachmentModel.AttachmentURL);
itm.AttachmentModel.AttachmentFile.SaveAs(path);
}
}
查看,
View,
<input type="hidden" value="" name="..."><input type="file" name="AttachmentFile">
模态,
Modal,
public HttpPostedFileBase AttachmentFile { get; set; }
public string AttachmentURL
{
get
{
return attachment;
}
set
{
attachment = value;
}
}
public string Attachment
{
get
{
return attachment;
}
set
{
attachment = value;
}
}
答
您好,我正在分享一个示例,以便在帖子上获取图像(文件)方法:
[HttpPost]
public ActionResult GetImage(HttpPostedFileBase文件)
{
//你可以把你现有的保存代码放在这里
if(file!= null&& file.ContentLength > 0)
{
//用文件做你想做的任何事情
}
}
希望它对你有所帮助。
Hello, I am sharing one example for get the image(file) on post method:
[HttpPost]
public ActionResult GetImage(HttpPostedFileBase file)
{
//you can put your existing save code here
if (file != null && file.ContentLength > 0)
{
//do whatever you want with the file
}
}
Hope it's helpful for you.