foreign-admin-ui/src/api/modules/upload.ts

27 lines
1.0 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { request } from '@/api'; // 引入你已封装的request
/**
* 图片上传接口(适配富文本编辑器)
* @param file 要上传的文件对象
* @returns Promise<{ url: string }> 图片上传后的URL
*/
export const uploadImageApi = (file: File): Promise<{ url: string }> => {
// 构建FormData图片上传必须用FormData格式
const formData = new FormData();
formData.append('image', file); // 对应后端接收字段名image
// 使用你已封装的post请求禁用loading富文本自身有状态提示
return request.post<{ url: string }>(
'/upload/image',
formData,
{
// 覆盖默认配置:图片上传需要特殊的请求头
headers: {
'Content-Type': 'multipart/form-data', // 表单上传必须的头
showLoading: false // 禁用全局loading避免和编辑器loading冲突
},
timeout: 30 * 1000, // 图片上传超时时间30秒
noRetry: false // 允许重试(网络波动时自动重试)
}
);
};