- <div class="uploadFile">
- <el-upload :file-list="fileList" :on-change="handleChange" :on-preview="handlePreview" :on-remove="handleRemove"
- :http-request="uploadFile" :auto-upload="false" class="upload" action="string" ref="upload" drag
- :multiple=false>
- <i class="el-icon-upload">i>
- <div class="el-upload__text">将文件拖到此处,或<em>点击上传em>div>
- el-upload>
- <el-button style="" size="small" type="success" @click="onSubmit">上传到服务器el-button>
- div>
- <script>
- import { uploadFile } from "@/utils/api";
- export default {
- data() {
- return {
- fileList: [],
- formData: "",
- }
- },
- created() { },
- mounted: function () {
- },
- methods: {
- submitUpload() {
- this.$refs.upload.submit();
- },
- handleRemove(file, fileList) {
- console.log(file, fileList);
- },
- handlePreview(file) {
- console.log(file);
- },
- delFile() {
- this.fileList = [];
- },
- handleChange(file, fileList) {
- this.fileList = fileList;
- },
- //自定义上传文件
- uploadFile(file) {
- //this.formData.append("file", file.file);
- },
- //删除文件
- handleRemove(file, fileList) {
- console.log(file, fileList);
- },
- // 点击文件
- handlePreview(file) {
- console.log(file);
- },
- //保存按钮
- async onSubmit() {
- let formData = new FormData();
- formData.append("file", this.fileList[0].raw);//拿到存在fileList的文件存放到formData中
- //formData.append("title", this.title);
- await hxn3bUploadFile(formData).then(res => {
- this.$message({
- message: res,
- type: 'success'
- });
- this.$emit("closeUpload")
- }).catch(
- res => {
- this.$message({
- message: res,
- type: 'error'
- });
- }
- )
- }
- }
- }
- script>
- <style scoped>
- .uploadFile {
- text-align: center;
- }
- style>
- import requestMultipart from './httpsMultipart'
-
- export function uploadFile(data) {
- return requestMultipart({
- url: '/api/upload',
- method: 'post',
- data
- })
- }
- const requestMultipart = axios.create({
- baseURL: process.env.VUE_APP_BASE_API,
- timeout: Config.timeout
-
- })
-
- requestMultipart.interceptors.request.use(
- config => {
- if (getToken()) {
- config.headers['Authorization'] = getToken()
- }
- config.headers['Content-Type'] = "multipart/form-data;charset=utf-8";
- return config
- },
- error => {
- Promise.reject(error)
- }
- )
-
- requestMultipart.interceptors.response.use(
- response => {
- return response.data
- },
- error => {
- if (error.response.data instanceof Blob && error.response.data.type.toLowerCase().indexOf('json') !== -1) {
- const reader = new FileReader()
- reader.readAsText(error.response.data, 'utf-8')
- reader.onload = function (e) {
- const errorMsg = JSON.parse(reader.result).message
- Notification.error({
- title: errorMsg,
- duration: 5000
- })
- }
- } else {
- let code = 0
- try {
- code = error.response.data.status
- } catch (e) {
- if (error.toString().indexOf('Error: timeout') !== -1) {
- Notification.error({
- title: '网络请求超时',
- duration: 5000
- })
- return Promise.reject(error)
- }
- }
- console.log(code)
- if (code) {
- if (code === 401) {
- store.dispatch('LogOut').then(() => {
- Cookies.set('point', 401)
- location.reload()
- })
- } else if (code === 403) {
- router.push({ path: '/401' })
- } else {
- const errorMsg = error.response.data.message
- if (errorMsg !== undefined) {
- Notification.error({
- title: errorMsg,
- duration: 5000
- })
- }
- }
- } else {
- Notification.error({
- title: '接口请求失败',
- duration: 5000
- })
- }
- }
- return Promise.reject(error)
- }
- )
- export default requestMultipart
- @PostMapping(具体路径)
- public String saveVue(String title,@RequestParam("file") MultipartFile file) throws IOException {
- //拿到具体文件 file
- return "SUCCESS";
- }