asp: Send files to the browser via Fileresult

Source: Internet
Author: User

FileResultis a abstract base class for all the others.

    • FileContentResult-You use it if you have a byte array of would like to return as a file
    • FilePathResult-When you had a file on disk and would like to return it's content (you give a path)
    • FileStreamResult-You had a stream open, you want to return it ' s content as a file

However, you ' ll rarely has to use these classes-you can just use one of the overloads and let ASP net MVC do the Controller.File Magic For you.

protected InternalFilepathresult File (stringFileName,stringcontentType);protected Internal VirtualFilepathresult File (stringFileName,stringContentType,stringfiledownloadname);protected InternalFilecontentresult File (byte[] filecontents,stringcontentType);protected Internal VirtualFilecontentresult File (byte[] filecontents,stringContentType,stringfiledownloadname);protected InternalFilestreamresult File (Stream fileStream,stringcontentType);protected Internal VirtualFilestreamresult File (Stream fileStream,stringContentType,stringFiledownloadname);
Filepathresult
 PublicActionResult FilePathDownload1 () {varPath = Server.MapPath ("~/files/barcodeconverter.exe"); returnFile (Path,"application/x-zip-compressed");} PublicActionResult FilePathDownload2 () {varPath = Server.MapPath ("~/files/barcodeconverter.exe"); returnFile ("G:\\barcodeconverter.exe","application/x-zip-compressed","BarcodeConverter.exe"); }  PublicActionResult FilePathDownload3 () {varPath = Server.MapPath ("~/files/barcodeconverter.exe"); varName =Path.getfilename (Path); returnFile (Path,"application/x-zip-compressed", name); }//FilePathDownload3 The file name after the download is still the default name for Action. The reason is that filedownloadname will be part of the URL and can contain only ASCII code. So, we need to Encode the name Url.encode PublicActionResult FilePathDownload4 () {varPath = Server.MapPath ("~/files/barcodeconverter.exe"); varName =Path.getfilename (Path); returnFile (Path,"application/x-zip-compressed", Url.encode (name)); }

Filecontentresult

Filecontentresult can send byte[] as a file directly to the browser (without creating a temporary file)

 Public Fileresult Download () {    byte[] filebytes = System.IO.File.ReadAllBytes (@ "c \ Folder\myfile.txt");     string " MyFile.txt " ;     return File (filebytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);}
Filestreamresult

It is not easy to find a suitable example for Filestreamresult, after all, the Http Response already contains a OutputStream attribute.

If you want to generate files dynamically, you can write data directly to this output stream, which is also highly efficient.

Of course, we do not write data directly to Response's outputstream in the Controller, which is incompatible with MVC, and we should encapsulate this action as a actionresult.

But think about it, the use of some, such as a compressed (or encrypted) file on the server, need to extract (or decrypt) sent to the user, or forward (or hotlinking)

(1) decompression (or decryption)
 PublicActionResult FileStreamDownload1 () {varPath = Server.MapPath ("~/files/myfile.zip"); varFileStream =NewFileStream (path, FileMode.Open); varZipinputstream =NewZipinputstream (FileStream); varEntry =Zipinputstream.getnextentry (); returnFile (Zipinputstream,"application/pdf", Url.encode (entry. Name);//Assume that there is only one file in the compressed file, and that it is in PDF format. }
(2) forwarding (or hotlinking)
Download the files on other websites as the site files (in fact, hotlinking):
 Public ActionResult FileStreamDownload1 () {    varnew WebClient (). OpenRead ("http://files.cnblogs.com/level/test.rar");     return " application/x-zip-compressed " " Test.rar " ); }

Bibliography: asp: Send files to the browser via Fileresult

asp: Send files to the browser via Fileresult

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.