问题
Before posting this message I looked many posts in SO but few where closed outright saying they need to see FAQ, few gave solutions that use iTextSharp or something else. But none solves my issue. My issue is I have a byte[] and I need to generate a PDF in new child window. We are just using ASP.NET MVC 4 and no iTextSharp or similar. Please let me know if there is already a post that exactly matches this. I am ok to create new Partial Views
I have one PDF Icon image in my Partial View. When user clicks it I need to display PDF in new browser window. I can successfully call a JavaScript function that calls the controller that gets the file from another server. I can even get the file converted to a byte array. I want to show this byte array in PDF format in a new browser window.
In View I have PDF Icon like below
<img onclick="ShowCDinPDF('@Url.Action("ShowPDF", "MyController", new {personid= personid} )','', '920','500')" />
ShowCDinPDF is in my javascript like below
function ShowCDinPDF(popUpURL, windowProperties, w, h) {
var childWindow = window.showModelessDialog(popUpURL, "", "");
}
In my Controller, I have below ShowPDF method
public ActionResult ShowPDF(string personid)
{
//call service and get data
string fileContent = response.FileContent;
byte[] data = Convert.FromBase64String(fileContent);
**// Here using data I need to show PDF in new window**
}
Please let me know how to create the PDF.
UPDATE
I made little progress. Now my code looks like below. A new window opens and I get error popup with message File does not begin with '%PDF-'. I tried to find solution to this but no success.
public ActionResult ShowPDF(string personid)
{
//call service and get data
string fileContent = response.FileContent;
byte[] data = Convert.FromBase64String(fileContent);
using (MemoryStream memoryStream = new MemoryStream())
{
Response.ClearHeaders();
Response.ClearContent();
Response.Charset = "";
Response.AddHeader("Content-Type", "application/pdf");
memoryStream.Write(data, 0, data.Length);
memoryStream.WriteTo(Response.OutputStream);
Response.Flush();
Response.Close();
Response.End();
}
return View();
}
Thanks in advance.
UPDATE 2
I tried alot but no use. Since we were approaching our PROD deadline, our team decided to create PDF file in our server and launch that file in IE browser.
NOTE
I really have no idea why this is down voted. Did anybody can render a file in PDF format without storing/creating the PDF file in any physical location. Why down vote?
回答1:
Assuming that the byte array you have represents a valid PDF then in your controller action you could serve this PDF by returning the proper ActionResult with the correct Content-Type response header:
public ActionResult ShowPDF(string personid)
{
//call service and get data
string fileContent = response.FileContent;
byte[] data = Convert.FromBase64String(fileContent);
**// Here using data I need to show PDF in new window**
return File(data, "application/pdf");
}
来源:https://stackoverflow.com/questions/32260041/asp-net-mvc-generate-pdf-from-byte