form upload file, ASP. NET Mvc5

Source: Internet
Author: User

    • Summary

This article is a summary of several ways to upload files at the front desk in mvc5.

    • Background code

_hostingenvironment for Ihostingenvironment to get the current program physical path

_hostingenvironment.webrootpath Get Path "program root directory/wwwroot"

[HttpPost]        Public ActionResult UploadFile (list<iformfile> files)        {            String name = Guid.NewGuid (). ToString ("n");            string FilePath = [email protected] "/files/";            foreach (var formfile in files)            {                string ext = System.IO.Path.GetExtension (formfile.filename);                System.IO.Directory.CreateDirectory (FilePath);                if (Formfile.length > 0)                {                    string path = FilePath + name + ext;                    using (var stream = new FileStream (path, FileMode.Create))                    {                        formfile.copytoasync (stream);}}            }            return redirecttoaction ("List");        }
    • Front Code
    1. Traditional form upload file
<form method= "POST" enctype= "Multipart/form-data" action= "@Url. Action (" UploadFile ")" >    <div class= " Form-group ">        <div class=" col-md-10 ">            <p> Select File </p>            <input type=" file "Name=" Files "multiple/>        </div>    </div>    <div class=" Form-group ">        <div class=" Col-md-10 ">            <input type=" Submit "value=" Upload "/>        </div>    </div></form>

2. Uploading files using Ajax

1 <formenctype= "Multipart/form-data">2     <Divclass= "Form-group">3         <Divclass= "Col-md-10">4             <P>Select File</P>5             <inputtype= "File"ID= "File"name= "Files"multiple/>6         </Div>7     </Div>8     <Divclass= "Form-group">9         <Divclass= "Col-md-10">Ten             <inputtype= "button"onclick= "Ajaxupload ()"value= "Ajax Upload" /> One             <inputtype= "button"onclick= "Formdataupload ()"value= "Formdata object uploads" /> A             <inputtype= "button"onclick= "UploadFile ()"value= "Upload" /> -         </Div> -     </Div> the </form> -  - <Script> -     functionajaxupload () { +         varform= NewFormData (Document.queryselector ("form")); - $.ajax ({ + URL:"@Url. Action ("UploadFile")", A Type:"POST", at Data:form, - ProcessData:false,  //do not process data - ContentType:false   //do not set content type -         }); -     } - </Script>
View Code

3.FormData Object File Upload

1 <Script>2     functionformdataupload () {3 4         varFormdata= NewFormData (Document.queryselector ("form"));5         varRequst= NewXMLHttpRequest ();6 Requst.open ("Post", "@Url. Action ("UploadFile")");7 requst.send (formdata);8     }9 </Script>
Formdata Object

Reference address

Https://developer.mozilla.org/zh-CN/docs/Web/API/FormData/Using_FormData_Objects

form upload file, ASP. NET Mvc5

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.