0

Please help me to fix my code why i can't use [HttpPost] in my action. Thanks a lot.

If not using [HttpPost], it work OK. If using [HttpPost] => error show "The resource cannot be found."

My code bellow:

View:

@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" />
}

Controllers:

[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();
        }
giathienphu
  • 137
  • 3
  • 14
  • check if this helps:http://stackoverflow.com/questions/25125127/asp-net-mvc-4-c-sharp-httppostedfilebase-how-do-i-store-file/25125284#25125284 – Ehsan Sajjad Aug 06 '14 at 03:46

2 Answers2

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;
    }
  • Have you tried using jquery to upload? I have a code sample for jquery and the handler if you would like to try some working methods I currently have with MVC Upload – user3842306 Aug 06 '14 at 03:26
0

Just re write your action with parameter as shown below. It may helps you

        [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();
        }
Razack
  • 950
  • 3
  • 13
  • 26