在 JavaScript 中创建和发送文件

Create and send file in JavaScript

本文关键字:文件 创建 JavaScript      更新时间:2023-09-26

如何在Internet Explorer 8中创建文件并将其发送到服务器?

现在我正在为 ie9(及更高版本)做这件事,所以:

var blob = new Blob([data], {//data is long xml-string
    type: "text/plain;charset=utf-8;"
});
var file = new FormData();
file.append('xml_file', blob, 'file.xml');
$.ajax({
    url: url,
    type: 'POST',
    data: file,
    contentType: false,
    processData: false,
    dataType: 'text',
    async: false,
    success: function (data) {
        console.log(data);
    },
    error: function (xhr) {
        console.log(xhr);
    }
});

看看这个,因为你使用的是jQuery。它使用起来非常简单,并且可以很好地与许多功能配合使用。

https://blueimp.github.io/jQuery-File-Upload/basic.html