Skip to content

uploadFile

上传文件

示例

ts
import { uploadFile } from '@base-web-kits/base-tools-web';

// 简单上传
const res = await uploadFile({ url: 'https://xx', file: file });

// 带额外参数和Header
const res = await uploadFile({
  url: 'https://xx',
  file: file,
  name: 'avatar', // 服务端接收的字段名
  data: { userId: 123 },
  header: { Authorization: 'Bearer xxx' },
});

// 监听上传进度
const res = await uploadFile(
  { url: 'https://xx', file: file },
  {
    onTaskReady: (task) => {
      // 监听进度
      task.onProgressUpdate((res) => {
        console.log('上传进度:', res.progress); // 0-100
        console.log('已上传:', res.loaded);
        console.log('总大小:', res.total);
      });
      // 取消上传
      // task.abort();
    },
    showLoading: '上传中...', // 显示Loading
    toastSuccess: '上传成功', // 成功提示
  },
);

// 直接返回json对象
type UploadResult = { status: number; message: string; data: { path: string } | null };

const json = await uploadFile<UploadResult>(
  { url: 'https://xx', file: file, responseType: 'json' },
);

// 默认 responseType 为 text,手动解析返回结果
console.log('uploadFile ok', JSON.parse(res));

参数说明

option (UploadFileOption)

参数名类型必填默认值说明
urlstring-上传接口地址
fileFile-要上传的文件对象
namestring'file'文件对应的 key (服务端通过这个 key 获取文件的二进制内容)
headerRecord<string, string | number>-请求头
dataUploadDataRecord<string, unknown>-请求参数;忽略 nullundefined,其余值通过 String(value) 写入 FormData
timeoutnumber0超时时间,单位 ms,默认 0(不超时)
responseType'text' | 'json''text'响应类型。'text' 返回原始字符串;'json' 会自动执行 JSON.parse 后返回对象

config (UploadConfig & WebApiConfig)

参数名类型必填默认值说明
onTaskReady(task: UploadTask) => void-获取 uploadTask 对象,可用于监听进度或取消上传
showLoadingboolean | stringfalse是否显示加载提示 (支持字符串作为自定义文本)
toastSuccessboolean | string | ((res) => boolean | string)false操作成功的 toast 提示
toastErrorboolean | string | ((err) => boolean | string)true是否显示操作失败的详细错误信息
showLogbooleantrue是否在控制台打印日志

responseType 说明

  • 默认 responseType: 'text',返回值类型为 string
  • 传入 responseType: 'json' 后,可配合泛型使用,自动将响应内容按 JSON 解析并返回对象
  • 如果 responseType: 'json' 但服务端返回的不是合法 JSON,会进入失败分支
  • 当上传失败且服务端返回了 JSON 错误体时,会优先读取其中的 message 作为错误信息
  • data 中的对象和数组不会自动 JSON 序列化;需要结构化参数时,请按服务端协议提前调用 JSON.stringify

UploadTask

onTaskReady 回调中获取的任务对象。

方法名类型说明
onProgressUpdate(callback: (res: UploadProgressEvent) => void) => void监听上传进度
abort() => void取消上传任务

UploadProgressEvent

属性名类型说明
progressnumber上传进度百分比: 0-100
loadednumber已上传字节数
totalnumber总字节数

版本

  • 1.2.0 新增