删除没用文件
parent
20ee40b6f2
commit
a13752b8bb
|
|
@ -1,24 +0,0 @@
|
||||||
export interface UploadFile {
|
|
||||||
/** 原始文件对象 */
|
|
||||||
originFileObj?: File;
|
|
||||||
[key: string]: any;
|
|
||||||
}
|
|
||||||
|
|
||||||
interface UseImportFileOptions {
|
|
||||||
/** 要上传的文件数组 */
|
|
||||||
files: UploadFile[];
|
|
||||||
/** 额外携带的参数对象 */
|
|
||||||
params?: Record<string, any>;
|
|
||||||
}
|
|
||||||
|
|
||||||
export interface ImportFileOptions {
|
|
||||||
url: string;
|
|
||||||
options: UseImportFileOptions;
|
|
||||||
}
|
|
||||||
|
|
||||||
export type ImportFileFunction = (url: string, options: UseImportFileOptions) => Promise<any>;
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 导入文件
|
|
||||||
*/
|
|
||||||
export default function useImportFile(): [boolean, ImportFileFunction];
|
|
||||||
|
|
@ -1,44 +0,0 @@
|
||||||
import {request} from "@cqsjjb/jjb-common-lib/http";
|
|
||||||
import { useState } from "react";
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 导入文件
|
|
||||||
*/
|
|
||||||
export default function useImportFile() {
|
|
||||||
// loading状态
|
|
||||||
const [loading, setLoading] = useState(false);
|
|
||||||
|
|
||||||
// 导入文件
|
|
||||||
const importFile = (url, options) => {
|
|
||||||
setLoading(true);
|
|
||||||
|
|
||||||
return new Promise((resolve, reject) => {
|
|
||||||
const { files = [], params = {} } = options
|
|
||||||
const formData = new FormData();
|
|
||||||
|
|
||||||
// 将文件添加到formData中
|
|
||||||
files.forEach((f) => {
|
|
||||||
f.originFileObj && formData.append("file", f.originFileObj);
|
|
||||||
});
|
|
||||||
|
|
||||||
// 将额外携带的参数添加到formData中
|
|
||||||
Object.keys(params).forEach((key) => {
|
|
||||||
formData.append(key, params[key]);
|
|
||||||
});
|
|
||||||
|
|
||||||
// 发送请求
|
|
||||||
request(url, "post", formData, { "Content-Type": "multipart/form-data" })
|
|
||||||
.then((res) => {
|
|
||||||
resolve(res);
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
reject(err);
|
|
||||||
})
|
|
||||||
.finally(() => {
|
|
||||||
setLoading(false);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
};
|
|
||||||
|
|
||||||
return [loading, importFile];
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue