前端js-put上传函数分享

export const fileCOSUpload = (files) => {
  return new Promise(async (resolve, reject) => {
    const { data, status  } = await axios.post(`xxxx`,  {
        RequestId: keyGuid(),
        DataType: 2,
        UserName: Cookie.get('ti-user'),
        FileName:files.name});
    // 获取到 Url 后,前端可以这样 ajax 上传
    let xhr = new XMLHttpRequest();
    // put上传
    xhr.open('PUT', data.Response.UploadAddress, true);
  // 请求头 xhr.setRequestHeader('X-TC-Titoken', Cookie.get('ti-token')); xhr.onload = (e) => { resolve(data.Response.FileId); }; xhr.onerror = (e) => { reject(null); }; xhr.send(files); // files 是要上传的文件对象 }) };

  

posted on 2021-01-06 17:14  忘忧很努力呀~  阅读(808)  评论(0编辑  收藏  举报