MVC4 - 无法在动作中使用[HttpPost]上传文件

时间:2014-08-06 03:14:31

标签: asp.net-mvc-4

请帮我修改我的代码为什么我不能在我的行动中使用[HttpPost]。非常感谢。

如果不使用[HttpPost],它可以正常工作。 如果使用[HttpPost] =>错误显示"The resource cannot be found."

我的代码如下:

查看:

@using (Html.BeginForm("Index", "ManageFiles", FormMethod.Post, new { enctype = "multipart/form-data" }))
{

    @Html.ValidationSummary(true)
    <input type="file" id="UpFile" name="UpFile" /><input type="submit" value="Start upload" />
}

控制器:

[HttpPost]
        public ActionResult Index()
        {
            var path = "~/images/upload/";
            // upload file
            try
            {
                var upload = Request.Files["UpFile"];
                if (upload != null && upload.ContentLength > 0)
                {
                        upload.SaveAs(Server.MapPath(path + upload.FileName));
                }
                else
                {
                    ModelState.AddModelError("", "The size of file must be between 0 and 2MB");
                }
            }
            catch
            {
                ModelState.AddModelError("", "Maybe file size too large");
            }
            // end upload file
            return View();
        }

2 个答案:

答案 0 :(得分:0)

    //Jquery


    function $UploadFile() {
                var fileUpload = $("#UpFile").get(0);
                var files = fileUpload.files;

                var data = new FormData();
                for (var i = 0; i < files.length; i++) {
                    data.append(files[i].name, files[i]);
                }

                var options = {};
                options.url = "UploadMyFile";
                options.type = "POST";
                options.data = data;
                options.contentType = false;
                options.processData = false;
                options.success = function (result) {
                   //Your success result here
                };
                options.error = function (err) { alert(err.statusText); };

                $.ajax(options);
            }
    $("submit").click(function(e){
    e.preventDefault();
    $UploadFile();
    });

    //MVC Action
    public ActionResult UploadMyFile(){
     var Request = context.Request;
                try
                {
                     var upload = Request.Files["UpFile"];
                    if (upload != null && upload.ContentLength > 0)
                    {
                            upload.SaveAs(Server.MapPath(path + upload.FileName));
return "Success";
                    }
                    else
                    {
                        ModelState.AddModelError("", "The size of file must be between 0 and 2MB");
                    }
                }
                catch (Exception ex)
                {
                    return "error: " + ex.Message; 
                }

    return null;
    }

答案 1 :(得分:0)

使用参数重新编写动作,如下所示。它可能会帮助你

        [HttpPost]
        public ActionResult Index(HttpPostedFileBase UpFile)
        {
            var path = "~/images/upload/";
            // upload file
            try
            {
                var upload = Request.Files["UpFile"];
                if (upload != null && upload.ContentLength > 0)
                {
                        upload.SaveAs(Server.MapPath(path + upload.FileName));
                }
                else
                {
                    ModelState.AddModelError("", "The size of file must be between 0 and 2MB");
                }
            }
            catch
            {
                ModelState.AddModelError("", "Maybe file size too large");
            }
            // end upload file
            return View();
        }