如何在Chrome / IE Edge中使用xhr.overrideMimeType? [英] how to use xhr.overrideMimeType in Chrome / IE Edge?

查看:389
本文介绍了如何在Chrome / IE Edge中使用xhr.overrideMimeType?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个发送文件的问题(表单数据格式请求的一部分)。



这个问题似乎来自 only Chrome 中为 Linux 发送文件(这是CVS文件,扩展名为.csv,基本上只是文本)与mimetype(Content-type in request body) Content-Type:application / octet-stream



因此,我试图覆盖 mimetype以匹配同样由 Linux text / csv 发送。 我的代码:

$ mimetype显然不是重写,仍然发送为 octet-stream 。 b
$ b

  let xhr = new XMLHttpRequest(); 
let form = new FormData();

form.append('file',file,file.name); //文件正确加载
form.append('payload',JSON.stringify(data)); //数据只是一个JSON对象

xhr.onreadystatechange = function(){
if(xhr.readyState === XMLHttpRequest.DONE){
//我们在这里到达Debian和Windows 10


xhr.upload.onerror = function(){....} //无错误

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 (普通二进制文件)的形式发送


解决方案

xhr.overrideMimeType 更改响应的MIME类型,而不是请求。



我想要更改文件的MIME类型,只需创建一个新的 Blob 与一个明确的文件类型:

  var blob = new Blob([blob],{type:'text / csv'}); 
form.append('file',blob,file.name);

上面的代码将上传格式的文件的MIME类型更改为text / csv根据需要。

PS。如果你真的想改变整个请求的MIME类型(而不仅仅是文件),可以使用 xhr.setRequestHeader('Content-Type','自定义MIME类型这里'); 。 (只有在 xhr.send 方法中发送非标准数据或自定义数据时,这才有意义)。


I have an issue with sending a file (part of a request in form data format).

The issue seems coming from the fact that only in Chrome for Linux the file (which is CVS file, with .csv extension and basically just text) is sent with mimetype (Content-type in request body) Content-Type: application/octet-stream

So, I am trying to override the mimetype to match the same sent by Chrome on Linux which is text/csv.

However the mimetype is apparently not overriden and still send as octet-stream.

My code:

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);

A couple of notes:

  • console.log(file.type) actually prints "text-csv" but only in Chrome for Linux (Debian specifically). in the other cases (any other browser or platform) nothing is printed
  • given the previous point, it seems clear to me for some reason any other browser / platform can't recognize the file type, so the file is sent as octet-stream (general binary file)

解决方案

xhr.overrideMimeType changes the MIME-type of the response, not the request.

I you want to change the MIME-type of the file, just create a new Blob with an explicit file type:

var blob = new Blob([blob], {type: 'text/csv'});
form.append('file', blob, file.name);

The above changes the MIME-type of the file in the uploaded form to "text/csv", as desired.

PS. If you literally want to change the MIME-type of the whole request (instead of just the file), use xhr.setRequestHeader('Content-Type', 'custom MIME type here');. (This only makes sense if you are sending a non-standard or custom data in the xhr.send method).

这篇关于如何在Chrome / IE Edge中使用xhr.overrideMimeType?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆