First, processing base64 data
Base64toFile (base64Data) {// Remove base64 header information and convert to byte let split = base64data.split (','); let bytes = window.atob(split[1]); Let fileType = split[0].match(/:(.*?) ; / [1]); Let ab = new ArrayBuffer(bytes.length); let ab = new ArrayBuffer(bytes.length); let ia = new Uint8Array(ab); for (let i = 0; i < bytes.length; i++) { ia[i] = bytes.charCodeAt(i); } return new Blob([ab], { type: fileType}); },Copy the code
Handle axios uploads
// create a new file instance.js import axios from 'axios'; Let instance = axios.create({headers: {' content-type ': 'multipart/form-data'}, // baseURL: '', // base path}); export default instance;Copy the code
Three, upload
import instance from '@/api/fileUpload.js'; Let formData = new formData (); let file = base64toFile(base64); // Add the file formdata.append ('file', file); // If there are other arguments formdata.append ('id', '1'); // call interface to Upload instance.post('/File/Upload',formData);Copy the code