【发布时间】:2016-08-27 01:03:47
【问题描述】:
我在下载和打开 pdf 文件时遇到错误
加载 PDF 文档失败
。但是当我尝试下载 txt 文件时,它会完全下载成功。我需要将此 ajax 请求作为 POST 方法,因此在互联网上搜索后,我找到了此代码。
$.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"); }});
我使用 web api 来下载文件
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;
}
请帮帮我
【问题讨论】:
-
如果你在
Blob构造函数中指定了mime类型呢?var blob = new Blob([data], { type: 'application/pdf' }); -
是的,我也试过 :(
-
你用的是什么浏览器?错误是在哪一行抛出的?
-
我正在使用 Google chrome。下载时我没有收到任何错误。 Pdf 文件被下载并在打开时显示错误 Failed to Load PDF
标签: c# jquery asp.net asp.net-web-api dotnetnuke