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:
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 Unrestricted Content-type Serveunknownfiletypes = 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, if we only need to increase . nupkg and. apk suffix files are downloaded, so you can add MIME types by using the following code, such as:
App. Usestaticfiles (New staticfileoptions { //serveunknownfiletypes = true Contenttypeprovider = new Fileextensioncontenttypeprovider (New dictionary<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 "> 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 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> 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></return S> public Iactionresult OnPostDown01 () {var addrurl = @ "D:\F\ learning \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>///httpclient For additional site file streams, then output///</summary>// <returns></returns> Public Async Task<iactionResult> OnPostDown02 () {var path = "Https://files.cnblogs.com/files/wangrudong003/%E7%89%B9%E4%BB%B7 01.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)); }
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;
Anxiety:
Netcore2.0 out still a while, 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 problems in the situation is still very little, not to worry about, of course, I know a few of the Daniel or the company's Some technical decision-makers are also concerned about and continue to use this to do new projects; Other cities I do not understand, on the Beijing side there are some start-up companies entrepreneurial project starting point is Netcore, so I hope that the waiting friends or leaders are time to start moving, together to promote the development of the community.