Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to upload and download Files in ASP.NET Core

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how to upload and download files in ASP.NET Core. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

1. Upload files using model binding (official example)

Address of official machine translation: https://docs.microsoft.com/en-us/aspnet/core/mvc/models/file-uploads

Here's a complaint-- this TM machine translation.. It would be better to read E by yourself.

First, we need to create an form form as follows:

Form form uploads multiple files:

Among them, asp-controller and asp-action, (this is the game of TagHelper, later) are the controllers and methods we are going to access.

Add the multiple attribute to our input tag to support multiple file uploads.

To create a controller, we write the upload method as follows:

Public async Task FileSave (List files)

{

Var files = Request.Form.Files

Long size = files.Sum (f = > f.Length)

String webRootPath = _ hostingEnvironment.WebRootPath

String contentRootPath = _ hostingEnvironment.ContentRootPath

Foreach (var formFile in files)

{

If (formFile.Length > 0)

{

String fileExt = GetFileExt (formFile.FileName); / / file extension without "."

Long fileSize = formFile.Length; / / gets the file size in bytes

String newFileName = System.Guid.NewGuid () .ToString () + "." + fileExt; / / randomly generate a new file name

Var filePath = webRootPath + "/ upload/" + newFileName

Using (var stream = new FileStream (filePath, FileMode.Create))

{

Await formFile.CopyToAsync (stream)

}

}

}

Return Ok (new {count = files.Count, size})

}

Here we use Asp.NET Core's new interface IFormFile. The specific definition of IFormFile is as follows:

Public interface IFormFile

{

String ContentType {get;}

String ContentDisposition {get;}

IHeaderDictionary Headers {get;}

Long Length {get;}

String Name {get;}

String FileName {get;}

Stream OpenReadStream ()

Void CopyTo (Stream target)

Task CopyToAsync (Stream target, CancellationToken cancellationToken = null)

}

The above code uses IHostingEnvironment to get the root address of the project.

The code injected into the constructor is as follows:

Private readonly IHostingEnvironment _ hostingEnvironment

Public UpLoadFileController (IHostingEnvironment hostingEnvironment)

{

_ hostingEnvironment = hostingEnvironment

}

In this way, we finished writing the controller, then went to the running front end and uploaded the file.. The effect is as follows:

Through IFormFile's CopyToAsync method, we can copy the file stream and save it locally.

two。 Upload files using Ajax

Above we use form upload, but most of the project will use Ajax for upload, so let's talk about how to use Ajax upload.

First, write the HTML code as follows:

AJAX uploads multiple files:

Download function downLoad () {var filename = $("# filename") .val (); _ window.location.href = "@ Url.Action (" DownLoad ")? file=" + filename;}

The effect is as shown in the figure:

On how to achieve file upload and download in ASP.NET Core to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report