"Failed to construct 'Blob': The provided value cannot be converted to a sequence" 下载文件时
"Failed to construct 'Blob': The provided value cannot be converted to a sequence" when downloading file
我正在尝试使用 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);
知道这有什么问题吗?
第一个参数应该是sequence。
因此,这将不起作用:
let blob = new Blob(data, {
type: "application/pdf"
});
但这将:
let blob = new Blob([data], {
type: "application/pdf"
});
这就是我最终得到的结果:
public HttpResponseMessage GetPdf()
{
var pdf = generatePdfByteArray();
var result = Request.CreateResponse(HttpStatusCode.OK);
var dataStream = new MemoryStream(pdf);
result.Content = new StreamContent(dataStream);
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "file.pdf"
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return result;
}
我正在尝试使用 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);
知道这有什么问题吗?
第一个参数应该是sequence。
因此,这将不起作用:
let blob = new Blob(data, {
type: "application/pdf"
});
但这将:
let blob = new Blob([data], {
type: "application/pdf"
});
这就是我最终得到的结果:
public HttpResponseMessage GetPdf()
{
var pdf = generatePdfByteArray();
var result = Request.CreateResponse(HttpStatusCode.OK);
var dataStream = new MemoryStream(pdf);
result.Content = new StreamContent(dataStream);
result.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment")
{
FileName = "file.pdf"
};
result.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");
return result;
}