❧ JS实现blob对象上传和远程读取
远程文件以二进制流blob对象读取
var url = "远程文件url";var xhr = new XMLHttpRequest();xhr.open('GET', url, true);xhr.responseType = "blob";xhr.onload = function() { if (this.status == 200) { var blob = this.response; // 将Blob对象转换成 ArrayBuffer var reader = new FileReader(); reader.readAsArrayBuffer(blob); reader.onload = function (e) { // 初始化项目 console.log(reader.result); } }}xhr.send();二进制blob对象上传
//动态创建表单对象var formData = new FormData();formData.append("file", blob,name); // blob 为文件二进制流对象,name 为文件名(可选参数)// 远程上传保存$.ajax({ url: 'http://localhost:8000/saveSb3', type: 'post', processData: false, contentType: false, data:formData}).done(function(res) { alert(JSON.stringify(res))}).fail(function(err) {});