Questions tagged [filecontentresult]

FileContentResult is an ActionResult in ASP.NET MVC that is used to send byte array as HTTP response.

FileContentResult is an ActionResult in ASP.NET MVC that is used to send byte array as HTTP response. This class is derived from FileResult base class. This ActionResult is useful in several scenarios such as:

  • The content of a file is served from a database,
  • The binary content is generated dynamically in memory e.g. dynamically generated image, PDF files etc.

To show 'Save As' dialog box by the browser, set FileDownloadName property value. If you set this value, the appropriate HTTP header will be added in response header:

Content-Disposition: attachment; filename=<FileDownloadName>

An example use could be:

public ActionResult GeneratePdf()
{
    byte[] byteContents = GetFileFromDatabase();
    return new FileContentResult(byteContents, contentType: "application/pdf")
    {
        FileDownloadName = "FileName.pdf"
    };
}
59 questions
63
votes
6 answers

Stream file using ASP.NET MVC FileContentResult in a browser with a name?

Is there a way to stream a file using ASP.NET MVC FileContentResult within the browser with a specific name? I have noticed that you can either have a FileDialog (Open/Save) or you can stream the file in a browser window, but then it will use the…
Anup Marwadi
  • 2,245
  • 4
  • 23
  • 39
11
votes
2 answers

Handling FileContentResult when file is not found

I have a controller action that downloads a file from an azure blob based on the container reference name (i.e. full path name of the file in the blob). The code looks something like this: public FileContentResult GetDocument(String pathName) { …
Alex R.
  • 4,394
  • 4
  • 28
  • 40
7
votes
4 answers

"File couldn't be downloaded" in Internet Explorer with ASP.NET MVC

So I'm returning a FileContentResult from an action like this: return File(pck.GetAsByteArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "MyExcelFile.xlsx"); When clicking "Open" in IE (I'm using IE9, version…
Matt Grande
  • 11,356
  • 6
  • 57
  • 83
7
votes
1 answer

Best way to render System.Drawing.Image in ASP.NET MVC 3 View

I have an object of type System.Drawing.Image and would like to display this image in a view. What would be the best way to do this? I have found some custom Html Helper methods that might fit the situation. Also found an example that uses a new…
d456
  • 1,047
  • 3
  • 13
  • 23
6
votes
3 answers

How can I call an MVC FileContentResult by Jquery and get it to prompt the user to save on it's return?

I'm generating a CSV from an MVC 3 website and using a FileContentResult to pass this to the user. This worked great but it took 30 seconds for the csv to generate and therefore 30 seconds before the prompt to save was given to the user. public…
5
votes
1 answer

Asp.net MVC FileContentResult - prevent opening in browser

One of my controller actions returns a file to the user. I would like the user to be presented with the download (open/save) dialog, regardless of the file type. This works fine when the file type is .doc, .docx, .xlsx, etc.., but when the file is…
yoozer8
  • 6,966
  • 6
  • 47
  • 84
5
votes
1 answer

Lots of unexpected "nul" caracters at the end of an xml with MemoryStream

I wrote an action for a user to downoad a generated xml generated whithout having to write it on the server disk, but keep it into memory. Here is my code : public FileContentResult MyAction() { MemoryStream myStream = new MemoryStream(); …
Sharpac
  • 378
  • 3
  • 4
  • 13
4
votes
0 answers

Set Page Title for a PDF inside the Action

I have an action that displays a pdf public ActionResult MyPdf() { var response = pdfService.MyPdf(new PdfRequest() { SiteId = siteSession.ActiveSiteId }); return File(response.Pdf, "application/pdf"); } The service opens a PDF, fills out…
Josh
  • 15,587
  • 25
  • 107
  • 149
4
votes
1 answer

ASP.NET MVC: Can an MSI file be returned via a FileContentResult without breaking the install package?

I'm using this code to return a FileContentResult with an MSI file for the user to download in my ASP.NET MVC controller: using (StreamReader reader = new StreamReader(@"c:\WixTest.msi")) { Byte[] bytes =…
Mike Pateras
  • 14,127
  • 29
  • 93
  • 131
4
votes
1 answer

ASP.NET MVC 4 FileContentResult runs twice

I'm currently trying to view images that are stored in a database as BLOBs, to do that I've created a Controller Action of the type FileContentResult which looks like this: public FileContentResult ImageFile(string imageId) { var client…
Erik Karlstrand
  • 1,360
  • 7
  • 19
3
votes
1 answer

MVC3 URL.Action not rendering my image in img tag

I have an image in my view, as: My controller, ItemController, has this method: public FileContentResult GetImage(){ var model = _itemService.GetItemImage(1); …
Jeff Reddy
  • 5,365
  • 9
  • 48
  • 86
2
votes
1 answer

ASP.NET MVC FileContentResult inline PDF filename not showing in browser title

.NET Framework 4.7.2 MVC project I have a controller action that dynamically loads a PDF to return inline to the browser. Everything is working, except the filename is not showing in the browser title bar or the tab. Instead, the browser is showing…
Bryan Williams
  • 135
  • 1
  • 11
2
votes
0 answers

Render FileContentResult using javascript

I have an Web API written in Core 2 that returns a FileContentResult. You need to do a GET with an Auth Token in the header for it to work. If you try it with postman, the file renders fine and it also renders well in an MVC View. Now we have an…
Nick
  • 2,745
  • 1
  • 27
  • 53
2
votes
1 answer

C# FileContentResult File Download In Custom Folder

I'm using FileContentResult method to download a generated PDF file. After running this code public FileContentResult genPDF(PDFFileData m) { try { var FL = generatePDF(m); var FLBytes =…
user6791278
2
votes
1 answer

MVC 5 FileContentResult Action Result Permission and Redirect

I have an MVC 5 application that allows users to download files that are stored in the database. I am using the FileContentResult action method to do this. I can restrict access to this method throughout the application, but a smart user can figure…
Cesar
  • 69
  • 2
  • 13
1
2 3 4