element上传大文件就报错500_VUE--ElementUI--文件上传(四)

本文介绍了如何在Vue中使用ElementUI进行文件上传,包括基本页面设置、删除、预览、下载功能,并展示了如何处理大文件上传时的500错误。同时,详细讲解了自定义上传操作和校验,以及与后端API的交互。
摘要由CSDN通过智能技术生成

一、文件上传

基本页面

action="http://localhost/goods-service/goods/sku/file"

list-type="picture-card"

:on-success="handleImageSuccess"

:before-upload="beforeImageUpload"

:auto-upload="true"

>

import { fileDelete,fileDownload } from "@/api/goods/fileUpload.js";

import { unescape } from "querystring";

export default {

data() {

return {

fileList: [],

dialogImageUrl: "",

dialogVisible: false,

disabled: false

};

},

methods: {

handleRemove(file) {

// 删除文件

console.log(file.response.data);

let fileNames = file.response.data.split("/");

fileDelete(fileNames[fileNames.length - 1]).then(res => {

if (res.data.code == "0000") {

this.fileList.splice(file, 1);

}

});

},

handlePictureCardPreview(file) {

// 预览文件

this.dialogImageUrl = file.url;

this.dialogVisible = true;

},

handleDownload(file) {

console.log("下载成功");

let fileNames = file.response.data.split("/");

let fileName = fileNames[fileNames.length - 1];

fileDownload(fileName).then(res => {

if (res.data.code == "0000") {

this.downloadFile(fileName,res.data.data);

}

});

},

handleImageSuccess(res, file, fileList) {

// 上传成功

this.fileList = fileList;

console.log("上传成功" + fileList.length);

},

beforeImageUpload(file) {

// 上传前格式与大小校验

const isJPG = file.type === "image/jpeg";

const isLt2M = file.size / 1024 / 1024 < 2;

if (!isJPG) {

this.$message.error("上传头像图片只能是 JPG 格式!");

}

if (!isLt2M) {

this.$message.error("上传头像图片大小不能超过 2MB!");

}

return isJPG && isLt2M;

},

// 下载

downloadFile(fileName, content) {

let aLink = document.createElement("a");

let blob = this.base64ToBlob(content); //new Blob([content]);

let evt = document.createEvent("HTMLEvents");

evt.initEvent("click", true, true); // initEvent 不加后两个参数在FF下会报错 事件类型,是否冒泡,是否阻止浏览器的默认行为

aLink.download = fileName;

aLink.href = URL.createObjectURL(blob);

// aLink.dispatchEvent(evt);

aLink.click();

},

// base64转blob

base64ToBlob(code) {

let parts = code.split(";base64,");

let contentType = parts[0].split(":")[1];

let raw = window.atob(parts[1]);

let rawLength = raw.length;

let uInt8Array = new Uint8Array(rawLength);

for (let i = 0; i < rawLength; ++i) {

uInt8Array[i] = raw.charCodeAt(i);

}

return new Blob([uInt8Array], { type: contentType });

}

}

};

自定义上传

页面添加:http-reques属性:

action属性必须存在,可以为空值。

action

:http-request="fileUpload"

JS调用:

// 一定要添加file默认参数

fileUpload(file) {

fileUpload(file).then(res => {

console.log(res)

});

}

API调用:

/**

* 自定义上传

* @param {*} fileobj

*/

export function fileUpload(fileobj){

let param = new FormData();

// 上传文件对象 名称file与后台控制器参数要一致

param.append('file',fileobj.file);

return request({

method: 'post',

// 上传地址

url: '/goods-service/goods/sku/file',

// 定义上传头

headers: {'Content-Type':'multipart/form-data'},

data: param

});

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值