XMLHttpRequest、FileReader 获取bob文件

  在对接网易云信视频通信的时候,遇到了bobUrl,本并不是专业的前端,又是第一次接触,查资料费了好大会儿功夫~   

        
       
1、使用 XMLHttpRequest 获取 Blob URL 源数据的 Blob 文件对象
        var bobUrl="blob:http://localhost/d23da49f-d3c5-4c48-8fe7-afb056fa6e37";
            var xhr = new XMLHttpRequest();
       var oMyForm = new FormData();
xhr.open('GET', bobUrl, true); xhr.responseType = 'blob'; xhr.onload = function(e) { if (this.status == 200) { var fileBlob = this.response; // blob is now the blob that the object URL pointed to. var fileType=fileBlob.type; var fileName=null; if (fileType.includes('webm')) { fileName=key+'.webm'; } console.log(fileBlob); //alert("----fileType---"+fileType); oMyForm.append('file',fileBlob,fileName); oMyForm.append("chatRecordId",beCalledInfo.custom); var fi=oMyForm.get('file'); console.log(fi); /* 用 XMLHttpRequest对象发送数据 */ var oReq = new XMLHttpRequest(); oReq.open("POST"," /admin/vedio/vedio_upload.do"); oReq.send(oMyForm); }else{ layer.msg("文件url解析失败,停止上传"); } }; xhr.send();

2、使用 FileReader 读取 Blob URL 源数据的 Blob 文件对象,具体请参考:https://developer.mozilla.org/en-US/docs/Web/API/FileReader
FormData 对象可参考:https://developer.mozilla.org/zh-CN/docs/Web/API/FormData/Using_FormData_Objects
后台Java request接收~

  

原文地址:https://www.cnblogs.com/binglan/p/11592573.html