asp.net-mvc 文件上传MVC
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/765211/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
File upload MVC
提问by Malcolm
With the following markup in my view:
在我看来,使用以下标记:
<form action="Categories/Upload" enctype="multipart/form-data" method="post">
<input type="file" name="Image">
<input type="submit" value"Save">
</form>
And in my controller:
在我的控制器中:
public ActionResult Upload(FormCollection form)
{
var file = form["Image"];
}
The value of file is null.
If I try it in a different view using a different controller Controller and it works with the same code.
文件的值为null. 如果我使用不同的控制器 Controller 在不同的视图中尝试它并且它使用相同的代码。
I have VS2008 on Vista, MVC 1.0.
我在 Vista、MVC 1.0 上有 VS2008。
Why?
为什么?
Malcolm
马尔科姆
回答by Daniel A. White
Use HttpPostedFileBaseas a parameter on your action. Also, add the AcceptVerbattribute is set to POST.
使用HttpPostedFileBase作为您的操作参数。此外,添加AcceptVerb属性设置为POST.
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Upload(HttpPostedFileBase image)
{
if ( image != null ) {
// do something
}
return View();
}
This code is quite in the spirit/design of ASP.NET MVC.
这段代码非常符合 ASP.NET MVC 的精神/设计。
回答by Brett Rigby
Not to be picky here or anything, but here's how the code ought to look, as Daniel is missing a few minor details in the code he's supplied...
不要在这里或任何东西挑剔,但这里是代码的外观,因为丹尼尔在他提供的代码中缺少一些小细节......
[AcceptVerbs(HttpVerbs.Post)]
public ActionResult UploadPlotImage(HttpPostedFileBase image)
{
if ( image != null )
{
// do something
}
return View();
}
回答by Pedro Santos
Try this code:
试试这个代码:
public ActionResult Upload()
{
foreach (string file in Request.Files)
{
var hpf = this.Request.Files[file];
if (hpf.ContentLength == 0)
{
continue;
}
string savedFileName = Path.Combine(
AppDomain.CurrentDomain.BaseDirectory, "PutYourUploadDirectoryHere");
savedFileName = Path.Combine(savedFileName, Path.GetFileName(hpf.FileName));
hpf.SaveAs(savedFileName);
}
...
}
回答by Shrilata Ellaboina
Even I was facing a problem , The value was null in image at
即使我遇到了问题,图像中的值为空
public ActionResult UploadPlotImadge(HttpPostedFileBase image)
Earlier I didn't add [AcceptVerbs(HttpVerbs.Post)]which I added. Even after adding it, it didn't work because the second part I was missing, enctype="multipart/form-data", needed to be in the form tag ..
早些时候我没有添加[AcceptVerbs(HttpVerbs.Post)]我添加的内容。即使在添加它之后,它也不起作用,因为我缺少的第二部分enctype="multipart/form-data",需要在表单标签中..
Now it works for me ....
现在它对我有用......
回答by Ram Khumana
try this class and below action and fix folder path in AppSetting.
尝试此类和以下操作并修复 AppSetting 中的文件夹路径。
config:
配置:
<appSettings>
<add key="UploadFolerPath" value="..Your folder path" />
</appSettings>
view:-
看法:-
<form action="Account/AddImage" id="form_AddImage" method="post" enctype="multipart/form-data">
<input type="file" id="Img" name="Img" class="required" />
<input type="submit" value="Upload" id="btnSubmit" />
</form>
Class:-
班级:-
public class FileUpload
{
public string SaveFileName
{
get;
set;
}
public bool SaveFile(HttpPostedFileBase file, string FullPath)
{
string FileName = Guid.NewGuid().ToString();
FileName = FileName + System.IO.Path.GetExtension(file.FileName);
SaveFileName = FileName;
file.SaveAs(FullPath + "/" + FileName);
return true;
}
}
//Post Action
//发布动作
[HttpPost]
public ActionResult AddImage(FormCollection Form)
{
FileUpload fileupload = new FileUpload();
var image="";
HttpPostedFileBase file = Request.Files["Img"];
if (file.FileName != null && file.FileName != "")
{
if (upload.ContentLength > 0)
{
fileupload.SaveFile(Request.Files["Img"], Server.MapPath(AppSetting.ReadAppSetting("UploadFolerPath")));
image = fileupload.SaveFileName;
// write here your Add/Save function
return Content(image);
}
}
else
{
//return....;
}
}

