适配最新的文件上传接口

This commit is contained in:
fangxiang 2023-02-14 17:40:59 +08:00
parent 5a1dba7a8f
commit a2102bdb5e
1 changed files with 13 additions and 115 deletions

View File

@ -347,14 +347,11 @@
:disable="uploader_loading" :disable="uploader_loading"
:url="getUrl" :url="getUrl"
multiple multiple
method="post" method="POST"
:headers="generatorFileUploadHeaders"
:label="$t('select file') + ':'" :label="$t('select file') + ':'"
:accept="uploader_accept" :accept="uploader_accept"
@start="onStartUpload" @start="onStartUpload"
@uploaded="onUploaded" @finish="onFailed"
@failed="onFailed"
@added="/*onFileAdded*/ null"
> >
</q-uploader> </q-uploader>
<q-space /> <q-space />
@ -882,21 +879,6 @@ export default defineComponent({
resolve = _resolve; resolve = _resolve;
}); });
}, },
generatorFileUploadHeaders(files: File[]) {
if (files.length > 0) {
return [
{
name: HttpProtocol.kHeaderXProductName,
value: HttpProtocol.getProductName($store),
},
{
name: HttpProtocol.kHeaderXFileLength,
value: files[0].size,
},
];
}
return [];
},
resetData() { resetData() {
loading.value = false; loading.value = false;
uploading.value = false; uploading.value = false;
@ -1068,8 +1050,8 @@ export default defineComponent({
getUrl(file: File[]) { getUrl(file: File[]) {
let url = GlobalData.getInstance().createCurrentRequestUrl(); let url = GlobalData.getInstance().createCurrentRequestUrl();
if (url) { if (url) {
url.pathname = HttpProtocol.RequestUploadFile; url.pathname = "/upload_media_file"; //HttpProtocol.RequestUploadFile;
url.searchParams.append("type", HttpProtocol.HttpUploadTypeNormal); // url.searchParams.append("type", HttpProtocol.HttpUploadTypeNormal);
url.searchParams.append("base_dir", path.value); url.searchParams.append("base_dir", path.value);
return url.toString(); return url.toString();
} }
@ -1078,89 +1060,6 @@ export default defineComponent({
onStartUpload() { onStartUpload() {
loading.value = true; loading.value = true;
}, },
async onFileAdded(files: File[] | File) {
setTimeout(async () => {
const onError = async (file: File | null) => {
await nextTick(() => {});
uploader.value.reset();
$q.notify({
color: "negative",
icon: "warning",
message: $t.t("load file error") + "!",
position: "top",
timeout: 3000,
});
};
let file: File | null = null;
try {
if (Array.isArray(files)) {
if (files.length > 0) {
file = files[0];
}
} else {
file = files;
}
if (file) {
const file_name = file.name;
const blob = file.slice(0, 1024 * 1024 * 1); // 1 MB
uploader_loading.value = true;
try {
let url = GlobalData.getInstance().createCurrentRequestUrl();
if (url) {
url.pathname = HttpProtocol.RequestPathCheckFileSupport;
// if (FileSuffixHelper.isVideoPath(file_name)) {
// }
const data = new FormData();
data.append(
"file",
new File([blob], file_name, {
lastModified: file.lastModified,
})
);
const response = await api.post(url.toString(), data, {
headers: {
"Content-Type": "multipart/form-data",
},
});
if (response && response.data && response.data.is_support) {
} else {
onError(file);
}
} else {
throw "";
}
} catch (e) {
console.error(e);
onError(file);
}
uploader_loading.value = false;
return;
}
} catch (e) {
console.error(e);
onError(file);
}
}, 1);
},
onUploaded(info: any) {
uploading.value = false;
uploader.value.reset();
refresh_file_list();
$q.notify({
type: "positive",
message: $t.t("file upload") + $t.t("success") + "!",
position: "top",
timeout: 1500,
});
loading.value = false;
},
async onFailed(info: any) { async onFailed(info: any) {
let file_list: any = await _request_list_files(path.value); let file_list: any = await _request_list_files(path.value);
setTimeout(() => { setTimeout(() => {
@ -1174,7 +1073,6 @@ export default defineComponent({
loading.value = false; loading.value = false;
} }
}, 500); }, 500);
}, },
copyFile(file: FileEntity) { copyFile(file: FileEntity) {
if (file) { if (file) {