js 使用 put 请求 上传文件 阿里云盘
作者:互联网
js 使用 put 请求 上传文件(阿里云盘)
<script src="https://cdn.bootcss.com/crypto-js/3.1.9-1/crypto-js.min.js"></script>
//文件hash值获取
function getHash(file) {
return new Promise((resolve, reject) => {
var reader = new FileReader();
reader.onload = function (event) {
var res = event.target.result;
fileData = new Blob([res])
res = CryptoJS.lib.WordArray.create(res);
var sha1 = CryptoJS.SHA1(res).toString();
resolve({
hash: sha1,
fileData: new Blob([res])
})
};
reader.readAsArrayBuffer(file);
})
}
//上传文件 url 阿里云盘 获取的上传路径 fileData 要上传的文件 blob
function uploadFile(url,fileData) {
return new Promise((resolve, reject) => {
let request = new XMLHttpRequest()
request.onreadystatechange = function () {
if (request.readyState == 4 && request.status == 200) {
resolve(request.getResponseHeader("etag"))
}
}
request.upload.onprogress = function (e) {
if (e.lengthComputable) {
console.log('进度:' + e.loaded / e.total)
}
}
request.open('PUT', url);
request.setRequestHeader('Content-type', '');
request.send(fileData);
})
}
getHash(file).then((data)=>{
... //获取上传文件地址省略
uploadFile(url,data.fileData).then((etag)=>{
//etag 用于合成文件的etag
})
})
标签:function,云盘,request,js,res,put,new,fileData,上传 来源: https://blog.csdn.net/weixin_47743108/article/details/115347155