提交数据以及文件上载

时间:2014-07-30 07:33:26

标签: asp.net-mvc-4

我正在尝试发布表单数据(例如文本框,复选框等)以及文件上载。我正在使用MVC。

有人给我一个解决方案吗?

3 个答案:

答案 0 :(得分:0)

您的Post Controller将如下所示:

 [HttpPost]
 public ActionResult YourController(YourModel model1, HttpPostedFileBase file)
 {
    if (file != null)
        {
            //here file variable will have the file which you have uploaded
        }
 }

HttpPostedFileBase包含您从View发布的文件。

在您的视图中,BeginForm()应如下所示:

Html.BeginForm(action, controller, FormMethod.Post, new { enctype="multipart/form-data"})

答案 1 :(得分:0)

使用MVC有很多方法可以做到这一点,如上所述强类型或这种方式也可以使用

[HttpPost]
public JsonResult CreateUpdate(FormCollection _formValues, YourModel _extraItem)
        {
           HttpPostedFileBase files = HttpContext.Request.Files;

           //do whatever u wish with ure files here
        }

希望有所帮助

答案 2 :(得分:0)

我使用XMLHttpRequest来解决此问题。非常感谢大家:D

相关问题