# 阿里云OSS文件上传

# ossUpload

ossUpload(serverUrl, blob, dir, useCache,index)

serverUrl:获取签名信息的接口地址
blob:图片文件数据
dir:文件存在在哪个目录下
useCache:是否启用缓存签名信息。单图上传无需缓存该信息,多图上传可以启用缓存。即使启用了缓存,缓存时间超过3秒,也会强制刷新
index:批量上传,或多人上传时,可以传入序号或其他标识,防止时间戳冲突

接口返回的数据必须符合如下格式:

{
	"success" :true,
	"data":{
		accessid: "",		//OSSAccessKeyId
		policy: "",			//
		signature: "",		//
		dir: "",			//默认文件存放目录,可不传
		host: "",			//图片上传成功后的主机头
		expire: 1522717312	//时间戳(秒),据此判断缓存是否过期
	}
}

# 实例

import {ossUpload} from 'sjfx'

uploadByOss(blob) {
  const xhr = new XMLHttpRequest();
  let fileName;
  xhr.onreadystatechange = () => {
    if (xhr.readyState === 4 && xhr.status === 200) {
      this.onUploadSuccess({
        imgUrl: 'http://sjfx-uploadfiles.oss-cn-beijing.aliyuncs.com/' + fileName,
        imgId: 0
      });
    }
  };
  xhr.upload.addEventListener('progress', (e) => {
    this.percentage = ~~(100 * e.loaded / e.total) / 2;
  }, false);

  ossUpload(tokenUrl, blob, 'gift', false).then((res) => {
    fileName = res.fileName;
    xhr.open('post', res.host);
    xhr.send(res.formData);
  });
}
Last Updated: 1/14/2020, 12:00:52 PM