zhangxiao
2024-08-20 e47b788ff5f5c699c682999c95da17eb284ca21d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import request from "@/packages/request";
import router from "@/packages/vue-router";
import { RequestOption } from "@arco-design/web-vue";
 
export function testList(params: Record<string, any>): Promise<{ title: string }> {
    return request({
        url: "https://jsonplaceholder.typicode.com/posts",
        params
    });
}
 
export function getVersion(): Promise<{ version: string }> {
    return request({
        url: `${location.origin}${router.options.history.base}/version.json`,
        notify: false
    });
}
 
/**
 * 基于arco-request的上传文件方法
 * @param option
 * @returns
 */
export function uploadFile(option: RequestOption): any {
    return new Promise((resolve, reject) => {
        const { onProgress, onError, onSuccess, fileItem, name } = option;
        const xhr = new XMLHttpRequest();
        xhr.withCredentials = false;
        if (xhr.upload) {
            xhr.upload.onprogress = function (event) {
                let percent = 0;
                if (event.total > 0) {
                    // 0 ~ 1
                    percent = event.loaded / event.total;
                }
                onProgress(percent, event);
            };
        }
        xhr.onerror = function error(e) {
            reject(e);
            onError(e);
        };
        xhr.onload = function onload() {
            if (xhr.status < 200 || xhr.status >= 300) {
                reject(xhr.responseText);
                return onError(xhr.responseText);
            }
            try {
                const res = JSON.parse(xhr.response);
                fileItem.url = res.data;
                resolve(res);
                onSuccess(res);
            } catch (e) {
                reject(e);
                return onError(e);
            }
        };
 
        const formData = new FormData();
        formData.append("file", fileItem.file as any);
        formData.append("step", "0");
        formData.append("type", "image/jpeg");
        xhr.open("post", "https://www.uhsea.com/Frontend/upload", true);
        xhr.send(formData);
    });
}
 
export function uploadFileNew(option: RequestOption): any {
    return new Promise((resolve, reject) => {
        const { onProgress, onError, onSuccess, fileItem } = option;
        const data = new FormData();
        //这里需要注意,后端接文件的name是什么,一般是image、file
        data.append("file", fileItem.file as any);
        request({
            url: "https://www.uhsea.com/Frontend/upload",
            method: "POST",
            headers: { "Content-Type": "multipart/form-data" },
            enableCancel: false,
            timeout: 0,
            data,
            onUploadProgress: (progressEvent) => {
                onProgress(progressEvent.loaded / progressEvent.total, progressEvent);
            }
        })
            .then((res) => {
                fileItem.url = res.data;
                onSuccess(res);
                resolve(res);
            })
            .catch((err) => {
                onError(err);
                reject(err);
            });
    });
}