当前位置:网站首页>File upload of native input tag
File upload of native input tag
2022-07-27 09:52:00 【Key】
<input type="file" @change="getFile" accept="application/vnd.openxmlformats-officedocument.spreadsheetml.sheet,application/vnd.ms-excel" />
// Get the upload file
const getFile = async (v) => {
console.log(v.target.files[0]);
let resultFile = v.target.files[0];
// adopt formData Save uploaded files
const formData = new FormData();
formData.append('file', resultFile);
let temp = await excelImport(formData);
console.log(temp);
if (temp) {
createMessage.success(' Successful import !');
reload();// Refresh table
}
};
/** * @description: Import reward records * @param {} param * @return {boolean} */
export const excelImport = async (param) => {
console.log(' Request parameters ', param);
const res = await BnUplodeRequest.post(API.excelImport, param);
console.log(' Return results ', res);
return res;
};
Package file upload axios
import axios, {
AxiosInstance, AxiosRequestConfig, AxiosResponse } from 'axios';
import Crypto from './Crypto';
import {
useAppStore } from '/@/store/modules/app';
import {
Constants } from '/@/Constants';
import {
useGlobSetting } from '/@/hooks/setting';
import Notification from '/@/bn/Notification';
import BnStorage from '/@/bn/BnStorage';
import Storage, {
StorageKey } from '/@/bn/BnStorage';
import qs from 'qs';
interface Result {
code: string;
message: string;
data: any;
encrypted: boolean;
}
class BnUplodeRequest {
private instance: AxiosInstance;
constructor() {
const serverUrl = useGlobSetting().serverUrl || '/';
console.log(serverUrl);
this.instance = axios.create({
baseURL: serverUrl,
timeout: 10000,
responseType: 'json',
headers: {
'content-type': 'multipart/form-data',
},
});
this.instance.interceptors.request.use(this.requestIntercept);
this.instance.interceptors.response.use(this.responseIntercept);
}
async post<T>(uri: string, formData: FormData): Promise<T> {
return this.request('POST', uri, formData);
}
private requestIntercept(request: AxiosRequestConfig): AxiosRequestConfig {
const appStore = useAppStore();
console.log(request);
const data = qs.stringify(request.params) || {
};
console.log(data);
const paramEncrypted: any = {
};
if (appStore.token) {
paramEncrypted.X_ACCESS_TOKEN = appStore.token;
}
request.params = paramEncrypted;
return request;
}
//setTimeOut For testing only
private responseIntercept(response: AxiosResponse): AxiosResponse {
const appStore = useAppStore();
const data: Result = response.data as Result;
if (data.code === Constants.REQUEST_SUCCESS) {
if (data.encrypted) {
data.data = Crypto.decrypt(data.data, appStore.keyInfo.key, appStore.keyInfo.iv);
}
try {
data.data = JSON.parse(data.data);
} catch (e) {
console.log(data.data + 'is Not a regular Json String');
}
return data.data;
}
throw new Error(data.message);
}
private async request(method: 'POST' | 'GET', uri: string, formData: FormData): Promise<any> {
return this.instance
.request({
url: uri,
method,
data: formData,
})
.catch(async (error) => {
if (axios.isAxiosError(error)) {
Notification.showNotice(' The network is abnormal or has other problems , Please try again later ');
} else if (error.message === ' Parameter decryption failed , The client secret key is wrong or expired ') {
Notification.showNotice(' Parameter decryption failed , Please login again ');
BnStorage.remove(StorageKey.KEY_INFO);
} else {
Notification.showNotice(error.message);
}
});
}
}
export default new BnUplodeRequest();
边栏推荐
- 一骑入秦川——浅聊Beego AutoRouter是如何工作
- Go Basics - arrays and slices
- Interview Essentials: shrimp skin server 15 consecutive questions
- LeetCode.565. 数组嵌套____暴力dfs->剪枝dfs->原地修改
- About getter/setter methods
- Summary of engineering material knowledge points (full)
- 省应急管理厅:广州可争取推广幼儿应急安全宣教经验
- Annotation and reflection
- [Wuhan University of technology] information sharing for the preliminary and second examinations of postgraduate entrance examination
- July training (day 23) - dictionary tree
猜你喜欢

C# 给Word每一页设置不同文字水印

XML概述

The command prompt cannot start mysql, prompting system error 5. Access denied. terms of settlement

为什么微服务一定要有API网关?

ACL2021最佳论文出炉,来自字节跳动

给自己写一个年终总结,新年快乐!

Lua function nested call

Brush the title "sword finger offer" day04

找工作 4 个月, 面试 15 家,拿到 3 个 offer

【云原生 • DevOps】一文掌握容器管理工具 Rancher
随机推荐
July training (day 14) - stack
2016 outlook
swagger-editor
July training (day 11) - matrix
[SCM]源码管理 - perforce 分支的锁定
语音直播系统——开发推送通知需要遵守的原则
C# 给Word每一页设置不同文字水印
July training (day 05) - double pointer
一骑入秦川——浅聊Beego AutoRouter是如何工作
What age are you still using date
面试京东 T5,被按在地上摩擦,鬼知道我经历了什么?
Google Earth engine app - maximum image synthesis analysis using S2 image
Interview JD T5, was pressed on the ground friction, who knows what I experienced?
Annotation and reflection
Towards the peak of life
关于getter/setter方法问题
刷题《剑指Offer》day03
July training (day 15) - depth first search
July training (day 13) - two way linked list
Final examination paper of engineering materials