File stream using ASP.NET MVC FileContentResult in a browser named?

Is there a way to transfer a file using ASP.NET MVC FileContentResult in a browser with a specific name?

I noticed that you can either have a FileDialog (Open / Save), or you can transfer the file in a browser window, but then it will use ActionName when trying to save the file.

I have the following script:

byte[] contents = DocumentServiceInstance.CreateDocument(orderId, EPrintTypes.Quote); result = File(contents, "application/pdf", String.Format("Quote{0}.pdf", orderId)); 

When I use this, I can transfer bytes, but the OPEN / SAVE dialog box is provided to the user. I would like to actually transfer this file in a browser window.

If I just use FilePathResult, it displays the file in a browser window, but then when I click the "Save" button to save the file in PDF format, it shows me the action name as the file name.

Has anyone come across this?

+56
asp.net-mvc filecontentresult
Jul 08 '10 at 18:16
source share
7 answers

This may be useful for those who are facing this problem. I finally understood the solution. It turns out that even if we use inline for "content-disposition" and specify the file name, browsers still do not use the file name. Instead, browsers try to interpret the file name based on the path / URL.

You can read further at this URL: Secret download of a file inside the browser with the correct file name

This gave me an idea, I just created my URL, which translates the URL and ends with the name of the file I would like to provide. So, for example, my initial call to the controller was only to pass the order identifier of the printed order. I expected the file name to be in the format Order {0} .pdf, where {0} is the order identifier. Similarly, for quotes, I need Quote {0} .pdf.

In my controller, I just went ahead and added an extra parameter to accept the file name. I passed the file name as a parameter in the URL.Action method.

Then I created a new route that would map this URL into the format: http: //localhost/ShoppingCart/PrintQuote/1054/Quote1054.pdf

 routes.MapRoute("", "{controller}/{action}/{orderId}/{fileName}", new { controller = "ShoppingCart", action = "PrintQuote" } , new string[] { "xxxControllers" } );
routes.MapRoute("", "{controller}/{action}/{orderId}/{fileName}", new { controller = "ShoppingCart", action = "PrintQuote" } , new string[] { "xxxControllers" } ); 

This pretty much solved my problem. Hope this helps someone!

Cheerz, Anup

+11
Jul 15 '10 at 5:16
source share
 public ActionResult Index() { byte[] contents = FetchPdfBytes(); return File(contents, "application/pdf", "test.pdf"); } 

and to open the PDF inside the browser you need to set the Content-Disposition header:

 public ActionResult Index() { byte[] contents = FetchPdfBytes(); Response.AddHeader("Content-Disposition", "inline; filename=test.pdf"); return File(contents, "application/pdf"); } 
+82
Jul 08 '10 at 19:37
source share

In fact, the easiest way is to do the following ...

 byte[] content = your_byte[]; FileContentResult result = new FileContentResult(content, "application/octet-stream") { FileDownloadName = "your_file_name" }; return result; 
+43
Apr 15 '13 at 10:34
source share

The previous answers are correct: adding a line ...

 Response.AddHeader("Content-Disposition", "inline; filename=[filename]"); 

... will cause multiple Content-Disposition headers to be sent to the browser. This happens when b / c FileContentResult internally applies a header if you provide it with a file name. An alternative and fairly simple solution is to simply subclass FileContentResult and override its ExecuteResult() method. Here is an example that creates an instance of the System.Net.Mime.ContentDisposition class (the same object that is used in the internal FileContentResult implementation) and passes it to the new class:

 public class FileContentResultWithContentDisposition : FileContentResult { private const string ContentDispositionHeaderName = "Content-Disposition"; public FileContentResultWithContentDisposition(byte[] fileContents, string contentType, ContentDisposition contentDisposition) : base(fileContents, contentType) { // check for null or invalid ctor arguments ContentDisposition = contentDisposition; } public ContentDisposition ContentDisposition { get; private set; } public override void ExecuteResult(ControllerContext context) { // check for null or invalid method argument ContentDisposition.FileName = ContentDisposition.FileName ?? FileDownloadName; var response = context.HttpContext.Response; response.ContentType = ContentType; response.AddHeader(ContentDispositionHeaderName, ContentDisposition.ToString()); WriteFile(response); } } 

In the Controller or in the Controller database, you can write a simple helper to create an instance of FileContentResultWithContentDisposition , and then call it from your action method, for example:

 protected virtual FileContentResult File(byte[] fileContents, string contentType, ContentDisposition contentDisposition) { var result = new FileContentResultWithContentDisposition(fileContents, contentType, contentDisposition); return result; } public ActionResult Report() { // get a reference to your document or file // in this example the report exposes properties for // the byte[] data and content-type of the document var report = ... return File(report.Data, report.ContentType, new ContentDisposition { Inline = true, FileName = report.FileName }); } 

Now the file will be sent to the browser with the selected file name and with the content-content header "inline; filename = [filename]".

I hope this helps!

+5
Jul 26 '13 at 23:07 on
source share

An absolutely simple way to stream a file to a browser using ASP.NET MVC is to:

 public ActionResult DownloadFile() { return File(@"c:\path\to\somefile.pdf", "application/pdf", "Your Filename.pdf"); } 

This is simpler than the method suggested by @ azarc3, โ€‹โ€‹since you donโ€™t even need to read bytes.

Credit: http://prideparrot.com/blog/archive/2012/8/uploading_and_returning_files#how_to_return_a_file_as_response

** Change **

Apparently, my โ€œanswerโ€ coincides with the OP question. But I do not encounter the problem that he is experiencing. This was probably a problem with the old version of ASP.NET MVC?

+4
Apr 01 '15 at 14:05
source share
 public FileContentResult GetImage(int productId) { Product prod = repository.Products.FirstOrDefault(p => p.ProductID == productId); if (prod != null) { return File(prod.ImageData, prod.ImageMimeType); } else { return null; } } 
0
Jul 26 '13 at 11:42 on
source share

How to export to Word document using Aspose.wrod method? Right now, here's how my PDF export method is:

  var tnr = (EventService)RemotingHelper.GetObject(typeof(EventService)); string test= tnr.CreateEventReportForEventList(eventIDs, false, null, true); byte[] eventData = System.Text.Encoding.UTF8.GetBytes(test); MemoryStream ms = new MemoryStream(eventData); Response.Cookies.Add(new HttpCookie("fileDownloadToken", "token")); var doc = new Aspose.Words.Document(ms); doc.Save(Response, "fileName.pdf", Aspose.Words.ContentDisposition.Attachment, null); Response.End(); 
0
Jan 26 '19 at 19:03
source share



All Articles