当前位置:网站首页>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();
边栏推荐
猜你喜欢
![WordPress prohibits login or registration of plug-ins with a specified user name [v1.0]](/img/94/92ad89751e746a18edf80296db9188.png)
WordPress prohibits login or registration of plug-ins with a specified user name [v1.0]

When I went to oppo for an interview, I got numb

Explain knative cloud function framework in simple terms!

Sentinel 万字教程 | 文末送书

35-Spark Streaming反压机制、Spark的数据倾斜的解决和Kylin的简单介绍

华为交换机双上行组网Smart-link配置指南

Sentinel ten thousand word tutorial | book delivery at the end of the text

二叉树习题总结

Redis 为什么这么快?Redis 的线程模型与 Redis 多线程

Snowflake vs. Databricks谁更胜一筹?2022年最新战报
随机推荐
Nacos配置中心动态刷新数据源
数据分析如何解决商业问题?这里有份超详细攻略
Understand chisel language. 23. Chisel sequential circuit (III) -- detailed explanation of chisel shift register
吃透Chisel语言.24.Chisel时序电路(四)——Chisel内存(Memory)详解
July training (day 17) - breadth first search
[Wuhan University of technology] information sharing for the preliminary and second examinations of postgraduate entrance examination
交换机端口镜像配置指南
达梦 PARTGROUPDEF是自定义的对象吗?
July training (day 14) - stack
刷题《剑指Offer》day04
7/26 思维+dp+后缀数组的学习
Review summary of engineering surveying examination
Voice live broadcast system - Principles to be followed in developing push notifications
Google Earth engine app - print the coordinates of points to the console and map, set the style and update it
LeetCode.814. 二叉树剪枝____DFS
July training (day 16) - queue
Simple use of tflite
July training (day 04) - greed
ACL2021最佳论文出炉,来自字节跳动
Brush the title "sword finger offer" day03