1.0.0 • Published 3 years ago

upload-files-service v1.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
3 years ago

安装

示例

import {
  UploadFile,
  UploadFileParamTypes,
  FileChangeParameter,
} from 'upload-files-service';

type CancelUpload = (cancelType?: string) => void; // cancelType 取消类型,仅作为标识符

/**
 * UploadFileParamTypes { 
        file: File; // 文件对象
        fileChange: (data: FileChangeParameter) => void; // 文件上传回调
        isVod?: boolean; // true-点播上传 false-对象上传
        isTranscode?: boolean; // 是否转码
    }
*/
type UploadFileCallback = (params: UploadFileParamTypes) => CancelUpload;

/**
 * env: dev-开发环境 test-测试环境 prod-生产环境
 * token: 用户token
 */
const uploadFile: UploadFileCallback = UploadFile(env, token);

let cancelUpload: CancelUpload = () => {};

/**
 * FileChangeParameter {
        status: string; // 状态 start - 开始, progress- 上传中, done - 上传完成,error - 上传失败  cance -- 取消上传
        message: string; // 返回信息
        path?: string; // 上传成功之后返回的 路径
        percent?: number; // 上传中 'progress' 返回的 上传进度
        data?: any; //文件服务返回的一整条数据
        cancelType?: string; // 取消类型
    }
*/

function onFileChange(e: any) {
  const files = e.target.files;
  if (files && files[0]) {
    cancelUpload = uploadFile({
      file: files[0],
      fileChange: (data: FileChangeParameter) => {},
    });
  }
}

// 取消上传
function onCancelUpload() {
  cancelUpload();
}

<form name='上传图片'>
  <input
    id='file'
    accept='.png'
    type='file'
    onChange={(e) => onFileChange(e)}
  ></input>
</form>;
<button onClick={onCancelUpload}>取消上传</button>;

更新日志

0.1.0

  • 上传文件
1.0.0

3 years ago