1

I am getting error while downloading and opening pdf files as

Failed to load PDF document

. But when i tried to download txt file it gets download success fully. I need to this ajax request to be as POST method, So after searching in internet i found this code.

$.ajax({
type: "POST",
url: url,
cache: false,
contentType: false,
processData: false,
success: function (data) {

    var blob = new Blob([data]);
    var link = document.createElement('a');
    link.href = window.URL.createObjectURL(blob);
    link.download = textName;
    link.click();
}, error: function (data) { alert("error"); }});

I use web api for downloading the file

public HttpResponseMessage Download(string fileName)
{   
    string filePath = Path.Combine(PATH, fileName.Replace('/', '\\'));
    byte[] pdf = System.IO.File.ReadAllBytes(filePath);
    HttpResponseMessage result = Request.CreateResponse(HttpStatusCode.OK);
    result.Content = new ByteArrayContent(pdf);
    result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
    result.Content.Headers.ContentDisposition.FileName = "MyPdf.pdf";
    result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
    return result;
}

Please help me

arun
  • 85
  • 2
  • 12
  • What if you specify the mime type in the `Blob` constructor? `var blob = new Blob([data], { type: 'application/pdf' });` – peco May 02 '16 at 10:57
  • yup , i tried that too :( – arun May 02 '16 at 11:47
  • What browser are you using? On what line is the error thrown? – peco May 02 '16 at 12:04
  • I am using **Google chrome**. I am not getting any error while downloading. Pdf file get downloaded and while opening that it shows error as **Failed to Load PDF** – arun May 02 '16 at 12:20

2 Answers2

0

Referencing Download pdf file using jquery ajax

jQuery has some issues loading binary data using AJAX requests, as it does not yet implement some HTML5 XHR v2 capabilities

It provides two options for you try. I won't repeat them here as they belong to that answer. Check the link.

On the server side, I've had success downloading PDF files using this method.

[HttpPost]
public HttpResponseMessage Download(string fileName)
{   
    string filePath = Path.Combine(PATH, fileName.Replace('/', '\\'));
    byte[] pdf = System.IO.File.ReadAllBytes(filePath);
    //content length for header
    var contentLength = pdf.Length;
    var statuscode = HttpStatusCode.OK;
    var result = Request.CreateResponse(statuscode);
    result.Content = new StreamContent(new MemoryStream(buffer));
    result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
    result.Content.Headers.ContentLength = contentLength;
    result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment");
    result.Content.Headers.ContentDisposition.FileName = "MyPdf.pdf";

    return result;
}

Not that much different to your original.

You should also check to make sure that the original file was not corrupt on the server if you are reading from disk/database or if you are dynamically generating the file.

Community
  • 1
  • 1
Nkosi
  • 235,767
  • 35
  • 427
  • 472
0

The problem is probably your Content-Disposition header. You need inline, but you have attachment.

result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("inline");

What you have now tells the browser to specifically send the file directly, and never attempt to display it in-browser. Chrome's internal PDF viewer currently chokes when you do this.

More: Content-Disposition:What are the differences between "inline" and "attachment"?

Community
  • 1
  • 1
Brian MacKay
  • 31,133
  • 17
  • 86
  • 125