MVC3 - How to output a file to download without saving it on the server first? - asp.net-mvc-3

In PHP, it is possible to return a file to the browser by echoing it out with the correct header. You do not need to save a copy of it prior on the server.
So assuming I have a bunch of data I wish to return as a excel file - after creating the data structure using OpenXML, how can I serve the file to the user without saving it on the server first?

It's pretty simple actually. In your controller you can have an action like this:
public ActionResult GetMyFile()
{
//dynamically generate a file
System.IO.MemoryStream ms;
ms = GenerateFile(); // Some function to return a memorystream object
// return the file
return File(ms.ToArray(), "filename.pdf");
}
The user will be presented with a dialog box asking if they want to open the file or save it.

Write your data to a stream and return it from your controller action method in a FileStreamResult by setting the FileStream, ContentType and FileDownloadName properties.
[HttpGet]
public FileStreamResult MyFile()
{
var fileStreamResult = new FileStreamResult (GetMyContentAsStream(), "my/content-type-here");
fileStreamResult.FileDownloadName = "my-file-download-name.here";
return fileStreamResult ;
}
Update:
A short cut for doing this, is to use the Controller.File() method.
[HttpGet]
public FileStreamResult MyFile()
{
return File(GetMyContentAsStream(), "my/content-type-here");
}

You can save the contents of your dynamically generated file in a MemoryStream object. When you return a file you can use MemoryStream's GetBuffer() method to pass an array of bytes as the first parameter. Then set ContentType and FileDownloadName parameters.
Regards

Related

webapi controller return csv data

I have a webapi controller code looks like:
[HttpPost]
public HttpResponseMessage DownloadForms(FormCriteria criteria)
{
string downloadData = new FormsToCsvHelper(Umbraco, criteria).GetCsv();
return Request.CreateResponse(HttpStatusCode.OK, downloadData);
}
If i look at the data returned in debugger just before sending it back it looks like:
Created Date,IP,Form Name,Email address,Message,Full Name
31/05/2019 10:43:08,127.0.0.1,Contact form - test,test12#hotmail.com,test,Ismail Mayat
If I copy and paste it into file and save as csv it looks fine in excel.
However the data i actually get back looks like:
"Created Date,IP,Form Name,Email address,Message,Full Name\r\n31/05/2019 10:43:08,127.0.0.1,Contact form - test,test12#hotmail.com,test,Ismail Mayat"
The whole record set it wrapped in quote and the line feed is seen as a literal so when opening file in excel its all on one line.
There is a DelegatingHandler that fire but that is for a specific url request so for this request it does not do anything.
Anyone any ideas whats going on?
WebAPI will serialize the value as JSON by default, so that's why your value is enclosed in double quotes.
To get around this, you can use the StringContent class:
return new HttpResponseMessage(statusCode) {
Content = new StringContent(downloadData, Encoding.UTF8, "text/csv")
};

How to download file using IHttpClientFactory in .Net Core 2.2?

In my web application (.net Core 2.2) I want to add the functionality of downloading a PDF file.
When the user clicks "download" - POST is sent to WebAPI and the server ask different server for PDF file by hardcoded URL (invisible to the user) and the file is passed to the user.
I was trying to use IHttpClientFactory
Startup:
services.AddHttpClient("demo", c =>
{
c.BaseAddress = new Uri("http://www.africau.edu/");
c.DefaultRequestHeaders.Add("Accept", "application/pdf");
});
Method for returns PDF file:
public async Task<FileStreamResult> GetPdfFile()
{
var client = _httpClientFactory.CreateClient("demo");
var uri = new UriBuilder(client.BaseAddress + "images/default/sample.pdf").Uri;
var stream = await client.GetStreamAsync(uri);
return new FileStreamResult(stream, new MediaTypeHeaderValue("application/pdf"));
}
Unfortunately it returns file download.json with: {"fileStream":{"needsDrain":true,"canRead":true,"canWrite":false,"canSeek":false
#EDIT
it seems that it's because I had Task<IActionResult> in my Controller instead of Task<FileStreamResult> but what if i prefer to returns ActionResult?
public async Task<IActionResult> GetPdf()
{
var result = await _service.GetPdfFile();
return Ok(result);
}
In your GetPdf method, you're passing a FileStreamResult to the Ok() method. This will serialize the FileStreamResult using the default serializer and send it to the client, hence why you're seeing the JSON.
Usually your service layer should not return an MVC WhateverResult object but rather an object intended to represent the actual data being retrieved - in your case this would be a PDF file. The easiest way to solve your particular case would probably be to do the following:
change GetPdfFile to return Task<FileStream> instead of Task<FileStreamResult>
remember to change its return statement to return stream;
change the return statement of GetPdf to return File(result, "application/pdf");
This will allow your service layer to return the PDF file as a stream, and then have your GetPdf endpoint return that stream as a file with the application/pdf file type.

What is the alternative for HttpContext.Response.OutputStream to use in WebAPI's HttpResponseMessage

I'm writing a WebAPI for handling PDF documents. It was written in a ashx page earlier implementing IHttpHandler and getting the context using HttpContext. I'm now writing it using WebAPI. In WebAPI we have HttpResponseMessage. For HttpContext.Response.BinaryWrite we have new ByteArrayContent in HttpResponseMessage. But what is the alternative for HttpContext.Response.OutputStream in WebAPI? I need to have the alternative of OutputStram in WebAPI because im passing this OutputStream as a parameter to another dll.
Code in ashx:
SomeReport.PdfReport rpt = new SomeReport.PdfReport(docID);
rpt.CreateReport(context.Response.OutputStream);
Actually you can use any stream for example MemoryStream but result should be wrapped into StreamContent.
public HttpResponseMessage Get()
{
var response = Request.CreateResponse();
var outputStream = new MemoryStream();
//write data to output stream
//or passing it to somewhere
outputStream.WriteByte(83);
outputStream.Position = 0;
response.Content = new StreamContent(outputStream);
return response;
}
If you need direct writing to output stream, please consider using PushStreamContent. Example

How to open generated PDF in MVC

I do AJAX call to generate PDF from crystal report.
Problem is , how to open PDF directly.
Below is my stuff. It looks, pdf is created but, it just return to view and not able to open PDF.
Please guide me.
Code:
public ActionResult CreatePDF(string paramValue)
{
DataTable dt = new DataTable();
DataColumn dc = new DataColumn("FieldName");
dt.Columns.Add(dcinitial);
DataRow dr = dt.NewRow();
dr[0]=paramValue;
dt.Rows.Add(dr);
ReportDocument oRpt = new ReportDocument();
string path = Server.MapPath("~/PDFDocs/crystalreport.rpt");
oRpt.Load(path);
oRpt.SetDataSource(dt);
MemoryStream oStream = new MemoryStream();
oStream = (MemoryStream)oRpt.ExportToStream(CrystalDecisions.Shared.ExportFormatType.PortableDocFormat);
Response.Clear();
Response.Buffer = true;
Response.ContentType = "application/pdf";
string fileName = "Report";
Response.AppendHeader("content-disposition", "attachment; filename=" + fileName);
Response.BinaryWrite(oStream.ToArray());
Response.End();
return View();
}
Thanks
Don't write to the Response in the controller. Aside from coupling issues and testability, it's confusing the functionality of that controller action. This code is trying to both write the file to the response and return the view.
Instead, just return the file, which you can do directly from the stream:
return File(oStream, "application/pdf", fileName);
However...
I do AJAX call
That's going to complicate things a bit. Ideally this wouldn't be an AJAX call. This is because AJAX doesn't handle "files" in a way you might think. There are options, but it would probably be easier to make this a normal page-level request so the browser can handle the file natively.
Since it's returning a file and not a view, the browser won't unload the current page. (Unless it's configured to display the file instead of save it, though there isn't much you can do about that. The server-side code is properly suggesting that the browser save the file by providing a Content-Disposition header.)

Returning an MVC FileContentResult to download .pdf file from

Hi I've search around for this quite a bit but I didn't find a situation that really resembled mine..hope I didn't miss a duplicate somewhere
The Goal: Return a file from a UNC share to the client as a download/open option.
Info: The share is located on a different server than the one hosting the web site. When a corresponding folder name on the menu is clicked, I am able to successfully read from the share (I return the files as a JSON result) and in Jquery I then append list items for each file found in the folder and make the list item ID's the filename. This works great.
When these appended list items are clicked on I pass their ID's (which are the filename, like "thefile.pdf") to the following controller which returns a FileContentResult.
files[0].ToString() below is similar to "\server\folder\"
public ActionResult OpenTheFile(string id)
{
List<string> files = new List<string>();
files.AddRange(Directory.GetFiles(LNFiles.ThePath, id, SearchOption.AllDirectories));
Response.AppendHeader("Content-Disposition", "attachment; filename=" + id + ";");
return File(files[0].ToString(), System.Net.Mime.MediaTypeNames.Application.Pdf, id);
}
And yes the obligatory "it works on my local machine". When deployed to the IIS 7.5 server and I click on the list item I get this YSOD error:
The handle is invalid. (Exception from
HRESULT: 0x80070006 (E_HANDLE))
I'm impersonating a user with rights to the file share...I'm at a loss, i was thinking something with encoding or screwed up rights? I've tried using a virtual dir instead but alas same issue.
In my case
changing this:
public ActionResult Download(int id)
{
var item = ItemRepo.GetItemById(id);
string path = Path.Combine(Server.MapPath("~/App_Data/Items"), item.Path);
return File(path, "application/octetstream", item.Path);
}
to this:
public ActionResult Download(int id)
{
var item = ItemRepo.GetItemById(id);
string path = Path.Combine(Server.MapPath("~/App_Data/Items"), item.Path);
return File(new FileStream(path, FileMode.Open), "application/octetstream", item.Path);
}
has worked. I am putting this here just in case anyone needs.
Check out this for a workaround
You may want to try a packet capture to see if you are receiving the same issue as documented here:
http://forums.asp.net/t/1473379.aspx/1
For your unc path - are you directly referencing \servername\share or are you using a network mapped drive letter?
God Bless you : ProgRockCode.
and since that involved an ActionResult, I wrote a custom ActionResult that used the "WriteFile" method.
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.WriteFile(FilePath, true);
context.HttpContext.Response.End();
}

Resources