ASP. NET core file upload and download

Source: Internet
Author: User

 Public classFilecontroller:controller {/// <summary> ///unlike ASP. NET WebForm and ASP. NET MVC, get the path of the project by injecting/// </summary>private ihostingenvironment _hostenv; public Filecontroller (Ihostingenvironment env) {_hostenv = env;//Other services} Publi C Iactionresult Index () {return View ();}/// <summary> ///mode one output text file/// </summary> /// <returns></returns>Public Iactionresult DownloadFile1 () {var buffer = Encoding.UTF8.GetBytes ("ASP. NET core download file"); return file (Buffer, "Text/plain", "file.txt"); } /// <summary> ///mode two output text file/// </summary> /// <returns></returns>Public Iactionresult DownloadFile2 () {var stream = new MemoryStream (); var streamWriter = new StreamWriter (stream); StreamWriter.Write ("{\" content\ ": \" ASP. NET Core download File\ "}"); StreamWriter.Flush (); Stream. Seek (0, Seekorigin.begin); Return File (Stream, "Text/plain", "File.json"); } /// <summary> ///mode three output picture file/// </summary> /// <returns></returns>Public Iactionresult DownloadFile3 () {var path = Path.Combine (_hostenv.contentrootpath, "UploadFile", " Netcore5.png "); var fileextensionname = path.getextension (Path); return PhysicalFile (Path, "Image/png", $ "{DateTime.Now.ToString (" Yyyymmddhhmmss ")}.{ Fileextensionname} "); } /// <summary> ///multiple file uploads/// </summary> /// <param name= "Files" >name of File upload</param> /// <returns></returns>Public Iactionresult UploadFile (IList<IFormFile>files) {if (Files! = null) {//handles multi-file foreach (var file in files) {////If file processing is required, you can filter by file extension var fileextensionname = Path.getex Tension (file. FileName). Substring (1); var Savefilepath = Path.Combine (_hostenv.contentrootpath, "UploadFile", $ "{DateTime.Now.ToString (" Yyyymmddhhmmss ")} . {fileextensionname} "); var stream = new FileStream (Savefilepath, FileMode.Create); ASP. NET core is good for asynchronous support, and if you use async you can use file. Copytoasync method file. CopyTo (stream); } return Ok (); } else {return Ok ();}} }"/file/uploadfile"Enctype="Multipart/form-data"Method="Post"> <input type="file"Name="Files"Multiple/> <input type="Submit"Value="Uploading Files"/> </form> </body> 

ASP. NET core file upload and download

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.