. NetCore download file,. netcore
This article will share with you. netCore provides two methods for downloading files: The A tag directs directly to the download file address and the post or get request background output file stream. This article will also focus on these two methods; if it is helpful to you, please provide more support.
- Allow the site to not recognize content-type download files (I .e., download is not subject to mime type restrictions)
- How can I download files with the suffix ".nupkg" and" .apk "?
- Example of the Razor template's post download file
- Feelings and anxiety about using NetCore over the past six months
Allow the site to not recognize content-type download files (I .e., download is not subject to mime type restrictions)
For the netcore web project, some built-in content-type file types are allowed to be downloaded. We will use a common razorweb project to see the example of downloading an excel file through a connection. First, create a bak folder in the wwwroot directory of the project and store the following files in the directory:
Then, you do not need to modify any code or settings to directly start the site, and then directly enter the download file address in the browser address bar, such:
Http: // localhost: 1120/bak/excel.xls
Http: // localhost: 1120/bak/love.apk
Http: // localhost: 1120/bak/stackexchange. redis.1.2.6.nupkg
Http: // localhost: 1120/bak/Startup. cs
During the test, only the excel.xls files can be directly downloaded. The other files are 404:
To get a suffix such as .apk and. nupkg. csFile is not restricted, We can use public static IApplicationBuilder UseStaticFiles (this IApplicationBuilder app, StaticFileOptions options); Extension to set, we just need to modify it to the following code:
App. UseStaticFiles (new StaticFileOptions {// The setting does not limit content-type ServeUnknownFileTypes = true });
Then, restart and run the program. At this time, we can access and download these files (You can download files with any suffix at this time.), As follows:
The files with the cs suffix are directly displayed in the google browser, so we will not map them here. If you are interested, try it;
How can I download files with the suffix ".nupkg" and" .apk "?
Through the above example, we can use ServeUnknownFileTypes = true; directly set the unrestricted download file type, which is usually not very good or not allowed, or is not often said unsafe; if we only need to add.Nupkgand .apk suffixesTo download the object, you can add the mime type through the following code, such:
app.UseStaticFiles(new StaticFileOptions { //ServeUnknownFileTypes = true ContentTypeProvider = new FileExtensionContentTypeProvider(new Dictionary<string, string> { { ".apk","application/vnd.android.package-archive"}, { ".nupkg","application/zip"} }) });
You can also download excel, apk, and nupkg files:
However, when we access http: // localhost: 1120/bak/Startup. cs, we will not be able to get the downloaded content:
Because we have not added the extension type for the. cs file, the system returns 404 to the system.FileExtensionContentTypeProviderThe object constructor transmits a dic ing dic type to let the project know the content-type files that can be downloaded;
Example of the Razor template's post download file
To be honest, I will study the Razor template when the last end is available. Next we will use her post form to request the method for downloading files from the backend. Below we will provide the login directly. code of the cshtml file:
@ Page @ model LoginModel @ {}< form method = "post"> <button type = "submit" asp-page-handler = "down" class = "btn"> download </button> <button type = "submit" asp-page-handler = "down01" class = "btn"> download 01 </button> <button type = "submit" asp- page-handler = "down02" class = "btn"> download 02 </button> </form>
It is worth noting that razor passesAsp-page-handler =To execute the request back-end method, let's see what the html code she generated finally looks like:
We can see thatHandlerExample ):
/// <Summary> /// download the Virtual File address output /// </summary> /// <returns> </returns> public IActionResult OnPostDown () {var addrUrl = "/bak/love.apk"; return File (addrUrl, "application/vnd. android. package-archive ", Path. getFileName (addrUrl ));} /// <summary> /// file stream output // </summary> /// <returns> </returns> public IActionResult OnPostDown01 () {var addrUrl = @ "D: \ F \ \ vs2017 \ netcore \ Study. aspNetCore \ WebApp02-1 \ wwwroot \ bak \ love.apk "; var stream = System. IO. file. openRead (addrUrl); return File (stream, "application/vnd. android. package-archive ", Path. getFileName (addrUrl);} // <summary> // get the file stream of another site through HttpClient, output // </summary> /// <returns> </returns> public async Task <IActionResult> OnPostDown02 () {var path =" https://files.cnblogs.com/files/wangrudong003/%E7%89%B9%E4%BB%B701.gif "; HttpClient client = new HttpClient (); client. baseAddress = new Uri (path); var stream = await client. getStreamAsync (path); return File (stream, "application/vnd. android. package-archive ", Path. getFileName (path ));}
The three post receiving methods at the backend are also used.FileStreamResultTo output the downloaded file, the difference is that the file source is different;
For simple websites, download files are generally stored in the site directory, which is similar to the wwwroot/bak directory here. Therefore, you can download files from the site virtual directory, that is, the first download method;
Some websites generally exist on the same server disk of the web site for File security. Therefore, you need to obtain the File stream through the second method and pass it to File ();
The last one is to convert the files on other websites or other websites as their own files for output. This method is what we often say.One of the leeching Methods;
For razor'sHandlerHere, we need to note that she corresponds to the xxx name in the OnGetxxx or OnPostxxx method of Our backend code. This is a razor request specification and must be followed.
Feelings and anxiety about using NetCore over the past six months
Thoughts:
Up to now, the latest version of netcore is 2.0, and its api is very powerful. According to the usage of several projects I have made, her api support is very good; some people have asked me if I have an api for image processing. The answer is yes. Now there are many types of packages in the nuget package community that support image processing, if you are interested, you can take a look at the idea (which corresponds to razor). It is not stressful to use it. It is recommended that some friends who do not dare to try netcore2.0 or who are interested in learning costs may wish to try it; netcore official documentation: https://docs.microsoft.com/en-us/aspnet/core/
Anxiety:
Some time has elapsed since netcore2.0. Although many friends have shared many related articles and git projects in the blog garden; however, in the three netqq groups, there are still few problems encountered in netcore development; of course, I know that some of the experts or some of the company's technical decision makers are also paying attention to and gradually use this to create new projects. I don't know about other cities, as for Beijing, netcore is used as the starting point of some entrepreneurial projects, so it is hoped that the friends or leaders who are still waiting to watch will start to take actions and work together to promote the development of the community.