File Upload ASP.NET MVC 3.0

后端 未结 21 1021
无人共我
无人共我 2020-11-22 01:09

(Preface: this question is about ASP.NET MVC 3.0 which was released in 2011, it is not about ASP.NET Core 3.0 which was released in 2019)

I want to

21条回答
  •  再見小時候
    2020-11-22 01:45

    Often you want to pass a viewmodel also, and not the only one file. In the code below you'll find some other useful features:

    • checking if the file has been attached
    • checking if file size is 0
    • checking if file size is above 4 MB
    • checking if file size is less than 100 bytes
    • checking file extensions

    It could be done via the following code:

    [HttpPost]
    public ActionResult Index(MyViewModel viewModel)
    {
        // if file's content length is zero or no files submitted
    
        if (Request.Files.Count != 1 || Request.Files[0].ContentLength == 0)
        {
            ModelState.AddModelError("uploadError", "File's length is zero, or no files found");
            return View(viewModel);
        }
    
        // check the file size (max 4 Mb)
    
        if (Request.Files[0].ContentLength > 1024 * 1024 * 4)
        {
            ModelState.AddModelError("uploadError", "File size can't exceed 4 MB");
            return View(viewModel);
        }
    
        // check the file size (min 100 bytes)
    
        if (Request.Files[0].ContentLength < 100)
        {
            ModelState.AddModelError("uploadError", "File size is too small");
            return View(viewModel);
        }
    
        // check file extension
    
        string extension = Path.GetExtension(Request.Files[0].FileName).ToLower();
    
        if (extension != ".pdf" && extension != ".doc" && extension != ".docx" && extension != ".rtf" && extension != ".txt")
        {
            ModelState.AddModelError("uploadError", "Supported file extensions: pdf, doc, docx, rtf, txt");
            return View(viewModel);
        }
    
        // extract only the filename
        var fileName = Path.GetFileName(Request.Files[0].FileName);
    
        // store the file inside ~/App_Data/uploads folder
        var path = Path.Combine(Server.MapPath("~/App_Data/uploads"), fileName);
    
        try
        {
            if (System.IO.File.Exists(path))
                System.IO.File.Delete(path);
    
            Request.Files[0].SaveAs(path);
        }
        catch (Exception)
        {
            ModelState.AddModelError("uploadError", "Can't save file to disk");
        }
    
        if(ModelState.IsValid)
        {
            // put your logic here
    
            return View("Success");
        }
    
        return View(viewModel);         
    }
    

    Make sure you have

    @Html.ValidationMessage("uploadError")
    

    in your view for validation errors.

    Also keep in mind that default maximum request length is 4MB (maxRequestLength = 4096), to upload larger files you have to change this parameter in web.config:

    
        
    

    (40960 = 40 MB here).

    Execution timeout is the whole number of seconds. You may want to change it to allow huge files uploads.

提交回复
热议问题