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"
    };
}
71 questions
72
votes
7 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,517
  • 4
  • 25
  • 42
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,664
  • 4
  • 30
  • 40
10
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,147
  • 4
  • 13
  • 25
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,964
  • 6
  • 62
  • 89
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…
6
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
  • 16,286
  • 25
  • 113
  • 158
6
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
  • 418
  • 3
  • 5
  • 13
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
  • 7,361
  • 7
  • 58
  • 93
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,715
  • 30
  • 97
  • 137
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,479
  • 9
  • 22
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,551
  • 9
  • 55
  • 88
3
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
  • 139
  • 2
  • 15
3
votes
1 answer

Display PDF(stored as a BLOB) as an image in MVC

I have to display a PDF(stored as BLOB in SQL Server table) in a partial view. I am doing the same for a ".jpeg" image stored as BLOB in the table.The image is getting displayed,however,the PDF isnt.I have the following code in my…
2
votes
0 answers

.Net Core Receive FileContentResult from one API to Another

I am having trouble finding information on how to do this so I will ask here. The situation is that I have an API that has a response type of FileContentResult. I have another API that needs to receive this FileContentResult. So, I guess the real…
TopBanana9000
  • 818
  • 2
  • 14
  • 32
2
votes
2 answers

how to open filecontentresult without window prompt in any browser

I have filecontentresult from controller action method as shown ,contents is byte[] type FileContentResult file= new FileContentResult(contents, "/PDF"); Response.AppendHeader("Content-Disposition", "inline; filename=" + filename); …
1
2 3 4 5