JS通过URL上传文件

点击查看详细内容

详细代码

  
      //url转blob转File上传文件   
  let url = baseInfo.businessLicenseUrl;
   var xhr = new XMLHttpRequest();
   xhr.open('GET', url);
   xhr.responseType = 'blob';
   xhr.onload = function() {
     var content = xhr.response;
     var blob = new Blob([content]);//blob.type=''
     var file2 = new File([blob], 'test.png', {type: 'image/png'});//
       console.log(file2)
       let formData = new FormData();
       formData.append('file',file2 );
       axios({
         method: 'post',
         url: `${urlConfig.uploadfile}`,
         data: formData,
         headers: {
           'Content-Type': 'multipart/form-data',
           'token': localStorage.getItem("token")
         }
       })
       .then(function (response) {
         that.setState({
           imgId:response.data.id,
           loading:false,
           imageUrl:response.data.url
         });
        setTimeout(() => {
        outValues.fileId=response.data.id,
         that.props.dispatch({ type: "selfInfo/saveSelfInfo", token: localStorage.getItem("username"),updateInfo: outValues});
       }, 500);     
       })
       .catch(function (error) {
         console.log(error);
       }); 

};
xhr.send();


原文地址:https://www.cnblogs.com/marvelousone/p/11190811.html