简体   繁体   English

Firefox Blob从文件中删除MIME类型

[英]Firefox Blob removes mime type from files

This used to work in Firefox prior to Firefox Quantum(v 57). 这曾经在Firefox Quantum(v 57)之前的Firefox中起作用。 The code still works in Chrome but not in Firefox anymore. 该代码仍可在Chrome浏览器中使用,但不再可在Firefox中使用。

User selects an image from his local computer, then creates a Blob with some JSON information and the file, like this: 用户从本地计算机选择一个图像,然后创建一个包含一些JSON信息和文件的Blob ,如下所示:

var fileElement = $("<input>").attr({
        "type": "file",
        "accept": "image/*"
    }).hide()
    .on("change", function () {
        var fileList = this.files;

        if (fileList.length > 0) {
            var file = fileList[0];
                    var str = JSON.stringify({
                        "uid": info.uid,
                        "utype": info.utype
                    });

                        var blob = new Blob([str.length, str, file]);
                        WebSocketConnection.sendMessage(blob);   

        }

    });

Then on the server side I extract the JSON string and get the information I want, then extract the mime type from the file and based on that mime type I save the file on server disk with one of this extensions [".png", ".jpg", ".jpeg", ".gif"] . 然后在服务器端,我提取JSON字符串并获取所需的信息,然后从文件中提取mime类型,并基于该mime类型,使用以下扩展名之一将文件保存在服务器磁盘上: [".png", ".jpg", ".jpeg", ".gif"]

Since Firefox quantum this is not working and the received mime type is application/octet-stream 由于Firefox Quantum不起作用,并且收到的mime类型为application/octet-stream

Is this a Firefox bug or did something changed in the api and I need to add some changes to my code? 这是Firefox错误还是在api中做了一些更改,我需要对代码进行一些更改?

I managed to make it work. 我设法使它起作用。 I still think this is a bug since it used to work in Firefox older versions. 我仍然认为这是一个错误,因为它曾经在Firefox旧版本中可以使用。

In the end I converted selected file to ArrayBuffer then created the Blob file with that ArrayBuffer . 最后,我将选定的文件转换为ArrayBuffer然后使用该ArrayBuffer创建了Blob文件。 I also updated the main question code to better understand what's happening. 我还更新了主要问题代码,以更好地了解正在发生的事情。

var fileElement = $("<input>").attr({
        "type": "file",
        "accept": "image/*"
    }).hide()
    .on("change", function () {
        var fileList = this.files;

        if (fileList.length > 0) {
            var file = fileList[0];
                    var str = JSON.stringify({
                        "uid": info.uid,
                        "utype": info.utype
                    });

                    var reader = new FileReader();
                    reader.onload = function () {
                        var arrayBuffer = this.result;
                        var blob = new Blob([str.length, str, arrayBuffer]);
                        WebSocketConnection.sendMessage(blob);                            
                    }
                    reader.readAsArrayBuffer(file);

        }

    });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM