我正在尝试使用ajax/jquery下载和保存PDF文件(我知道...)。
这是我在服务器端的代码:
public HttpResponseMessage GetPdf()
{
var pdf = generatePdfByteArray(); // byte[]
var result = Request.CreateResponse(HttpStatusCode.OK);
result.Content = new ByteArrayContent(pdf);
//result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
//{
// FileName = "blah.pdf"
//};
// tried with and without content disposition.. shouldn't matter, i think?
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return result;
}
这是客户端代码:
let ajaxOptions = {
url: '/url',
type: "GET",
accepts: "application/pdf",
success: (data) => {
let blob = new Blob(data, {
type: "application/pdf"
}); // <-- this fails
// stuff...
}
};
$.ajax(ajaxOptions);
您知道这是什么问题吗?