【发布时间】:2016-12-02 22:37:58
【问题描述】:
我在发送文件(表单数据格式的请求的一部分)时遇到问题。
问题似乎来自于 Chrome for Linux 中的 only 文件(它是 CVS 文件,扩展名为 .csv 和基本上只是文本)通过 mimetype(请求正文中的内容类型)发送 Content-Type: application/octet-stream
所以,我试图覆盖 mimetype 以匹配 Chrome 在 Linux 上发送的相同类型,即 text/csv 。
但是,mimetype 显然没有被覆盖,仍然作为 octet-stream 发送。
我的代码:
let xhr = new XMLHttpRequest();
let form = new FormData();
form.append('file', file, file.name); // the file is loaded correctly
form.append('payload', JSON.stringify(data)); // data is just a JSON object
xhr.onreadystatechange = function() {
if (xhr.readyState === XMLHttpRequest.DONE) {
// we arrive here both on Debian and Windows 10
}
}
xhr.upload.onerror = function() { .... } // no error
xhr.open('POST', 'http://<my_url>', true);
console.log(file.type);
xhr.overrideMimeType("text/csv");
xhr.send(form);
几点说明:
- console.log(file.type) 实际上打印“text-csv”,但仅在 Chrome for Linux(特别是 Debian) 中。在其他情况下(任何其他浏览器或平台)不打印任何内容
- 鉴于前面的观点,出于某种原因,我似乎很清楚任何其他浏览器/平台无法识别文件类型,因此文件作为 octet-stream 发送strong>(通用二进制文件)
【问题讨论】:
标签: google-chrome xmlhttprequest