This article will share with you the silk. Netcore Download the file, there are two common downloads: a tag directly points to the download file address and post or GET request background output file stream way, this article will also be around these two to share; If you have a good help, please support a lot.
- Allow the site to not recognize content-type download files (i.e., download not restricted by MIME type)
- How to allow files to download. nupkg and. APK suffixes
- Example of post download file for Razor template
- Half a year to use netcore some feelings and anxiety
Allow the site to not recognize content-type download files (i.e., download not restricted by MIME type)
For Netcore Web projects, there are some types of files that content-type allow to download, and we'll take a look at an ordinary Razorweb project to download the Excel example directly from the connection; First, create a Bak folder in the project's Wwwroot directory , and then store the following files in this directory:
Then do not modify any code or settings, directly start the site, and then directly in the browser address bar to enter the download file address, such as:
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
By testing this time only Excel.xls files can be downloaded directly, the others are 404:
To apk,.nupkg.cs files such as suffixes are not restricted , we can use the 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 { // set unlimited content-type True });
Then in the restart run, this time we will visit the download of these files is no problem ( Note that this time to download any suffix files are ok ), as follows:
As for the CS suffix file in Google Browser is directly displayed content, here is not the map, interested can try;
how to allow files to download. nupkg and. apk suffixes
Using the above example we can use serveunknownfiletypes = true; directly set unlimited download file types, which is usually not too good or not allowed, or not often said unsafe it If we only need to increase the download of the . nupkg and. apk suffix files, you can add MIME types by using the following code, such as:
App. Usestaticfiles (Newstaticfileoptions {//Serveunknownfiletypes = TrueContenttypeprovider =NewFileextensioncontenttypeprovider (Newdictionary<string,string> { { ". apk","application/vnd.android.package-archive"}, { ". Nupkg","Application/zip"} }) });
The same can be downloaded for excel,apk,nupkg suffix files:
But this time we access http://Localhost:1120/bak/startup.cs will not get the downloaded content:
Because we did not add the extension type to the. cs file, the system directly returns to us 404; Here we pass Fileextensioncontenttypeprovider The object's constructor passes a mapping dic type to let the project know the files that are allowed to download the Content-type type;
Example of post download file for Razor template
To be honest, I'll be studying the next razor template when I'm free at one end of the day, and we'll use her post form to request the backend to download the file, and the code for the login.cshtml file directly below:
@page @model loginmodel@{}<form method="Post"> <button type="Submit"Asp-page-handler=" Down" class="btn"> Downloads </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 here that razor through the asp-page-handler= to execute the method of the back end of the request, let's see what the final HTML code will look like after she generates it:
Can see here mainly through the handler as the parameter name to pass the request of the back-end method, and then look at the back-end code so write (in order to facilitate the download of the file of the road I take love.apk as an example):
/// <summary> ///virtual file address output download/// </summary> /// <returns></returns> PublicIactionresult Onpostdown () {varAddrurl ="/bak/love.apk"; returnFile (Addrurl,"application/vnd.android.package-archive", Path.getfilename (Addrurl)); } /// <summary> ///how the file stream is exported/// </summary> /// <returns></returns> PublicIactionresult OnPostDown01 () {varAddrurl =@"D:\F\ Learning \vs2017\netcore\study.aspnetcore\webapp02-1\wwwroot\bak\love.apk"; varstream =System.IO.File.OpenRead (Addrurl); returnFile (Stream,"application/vnd.android.package-archive", Path.getfilename (Addrurl)); } /// <summary> ///get another site's file stream through HttpClient, and then output/// </summary> /// <returns></returns> Public AsyncTask<iactionresult>OnPostDown02 () {varPath ="Https://files.cnblogs.com/files/wangrudong003/%E7%89%B9%E4%BB%B701.gif"; HttpClient Client=NewHttpClient (); Client. BaseAddress=NewUri (path); varstream =awaitclient. Getstreamasync (path); returnFile (Stream,"application/vnd.android.package-archive", Path.getfilename (Path)); }
Back-end 3 post acceptance methods are also used Filestreamresult to output the download file, the difference is that the file source is different;
For some simple sites, download files are usually found in the site directory, a bit similar to my Wwwroot/bak directory here, so can be downloaded through the site virtual directory, that is, the first way to download;
Some sites for file security, usually exist in the Web site of the same server disk, so need to get the file stream through the second way here, and then passed to file ();
The last one is to own other sites or other people on the site of the file to turn, as their own files to output, this is the way I often say one of the hotlinking way ;
For Razor Handler parameters, it is important to note that she corresponds to the back-end code Ongetxxx or Onpostxxx method of the xxx name, which is a kind of razor request specification, must obey OH.
Half a year to use netcore some feelings and anxiety
Feelings:
To the current Netcore the latest version of 2.0, its API is very strong, for the current I do a few projects used to see, she API support is very good; before I met someone asked me to deal with the API of the picture, the answer is yes, now the NuGet pack community has many kinds of package support image processing, interested friends can go to see the HTTP S://www.nuget.org/packages, after a few projects, deep feeling netcore learning cost is not high, Netcore MVC project, as long as the MVC framework, or webform (personal feeling corresponding razor) It is no pressure to use, it is suggested that some people who do not dare to try netcore2.0 or feel the cost of learning friends may wish to try; Netcore Official documents: https://docs.microsoft.com/en-us/aspnet/core/
Anxiety:
Netcore2.0 out still a while, this time although a lot of friends in the blog Park to share a lot of related articles and Git projects, but in the 3 NETQQ group reaction or proposed Netcore development encountered problem situation is still very little, not to worry about, of course, I know a few of the Daniel or the company's part of the technology The people at the decision-making level are also concerned and are using this to do new projects; Other cities I do not understand, on the Beijing side of the start-up of a number of start-up companies are using Netcore, so hope is still watching friends or leaders are time to start moving, together to promote the development of the community.
. Download documents under Netcore