我最近问了另一个(相关的)问题,这导致了这个后续问题: 提交数据而不是输入表单的文件
通过jQuery.ajax()文档(http://api.jquery.com/jQuery.ajax/)阅读,似乎接受的dataTypes列表不包含图像.
我正在尝试使用jQuery.get(或jQuery.ajax,如果必须)检索图像,将此图像存储在Blob中并将其上传到POST请求中的另一台服务器.目前,看起来由于数据类型不匹配,我的图像最终被破坏(大小以字节不匹配等).
执行此操作的代码如下(它在coffeescript中但不应该难以解析):
handler = (data,status) ->
fd = new FormData
fd.append("file", new Blob([data], { "type" : "image/png" }))
jQuery.ajax {
url: target_url,
data: fd,
processData: false,
contentType: "multipart/form-data",
type: "POST",
complete: (xhr,status) ->
console.log xhr.status
console.log xhr.statusCode
console.log xhr.responseText
}
jQuery.get(image_source_url, null, handler)
Run Code Online (Sandbox Code Playgroud)
如何将此图像检索为blob?
我在 Angular 6 中使用 ExcelJs Lib(1.12.0),但是这个 Lib 在开发中工作正常,但在生产中却不行,它在 workbook.xlsx.writeBuffer() 点失败。
import * as ExcelJS from "exceljs/dist/exceljs.min.js";
downloadExcelFile() {
const title = 'Titlename';
const header = 'headerdata'
const data = 'actualData';
let workbook = new ExcelJS.Workbook();
let worksheet = workbook.addWorksheet(title);
worksheet.addRow(header);
for(let d of data) {
worksheet.addRow(d);
}
workbook.xlsx.writeBuffer().then((data) => {
let blob = new Blob([data], { type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' });
const anchor = document.createElement('a');
const url = URL.createObjectURL(blob);
anchor.href = url;
anchor.download = title + '.xlsx';
document.body.appendChild(anchor);
anchor.click();
document.body.removeChild(anchor);
URL.revokeObjectURL(url);
}).catch(err …Run Code Online (Sandbox Code Playgroud)