Asp.Net MVC 中的文件上传
我正在尝试在我的 MVC 项目中制作一个文件上传页面。首先我想在本地管理这个。 我的问题是: 1-这是我的控制器和视图。我需要做些什么才能使这段代码正常工作吗?我的意思是定义一个模型或使用jquery等。上传文件时的过程是什么?
[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase uploadFile)
{
if (uploadFile.ContentLength > 0)
{
string filePath = Path.Combine(HttpContext.Server.MapPath("C:/Users/marti/../PhotoGallery/myimages"),
Path.GetFileName(uploadFile.FileName));
uploadFile.SaveAs(filePath);
}
return View();
}
这是视图:
<input name="uploadFile" type="file" />
<input type="submit" value="Upload File" />
2-当我调试它时,它永远不会进入控制器。
I am trying to make a file uploading page in my MVC project. First of all i want to manage this locally.
My Questions are:
1- Here is my controller and view. Is there any thing that I have to do to make this code work? I mean defining a model or using jquery, etc. What is the process when a file is being uploaded?
[HttpPost]
public ActionResult FileUpload(HttpPostedFileBase uploadFile)
{
if (uploadFile.ContentLength > 0)
{
string filePath = Path.Combine(HttpContext.Server.MapPath("C:/Users/marti/../PhotoGallery/myimages"),
Path.GetFileName(uploadFile.FileName));
uploadFile.SaveAs(filePath);
}
return View();
}
Here is the View:
<input name="uploadFile" type="file" />
<input type="submit" value="Upload File" />
2- When i debug this, It never goes to controller.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的视图表单上可能需要
enctype='multipart/form-data'
:这是我的控制器代码:
其中有一些自定义位,因此您可以忽略它们。你需要的东西应该在那里。
华泰
You may need the
enctype='multipart/form-data'
on your view form:And here is my controller code:
There are some custom bits in there so you can ignore those. The gyst of what you need should be there.
HTH