上传和下载
提示:如有错误,请指出,定会修改
一、canvas图片下载
/**
* canvas下载图片
* @param item 图片信息(名称等)
* @returns {*}
*/
downloadClick(item) {
var image = new Image()
image.setAttribute('crossOrigin', 'anonymous')
image.onload = function () {
var canvas = document.createElement('canvas')
canvas.width = image.width
canvas.height = image.height
var context = canvas.getContext('2d')
context.drawImage(image, 0, 0, image.width, image.height)
var url = canvas.toDataURL('image/png')
var a = document.createElement('a')
var event = new MouseEvent('click')
if (item.col1) {
a.download = item.col1;
} else {
a.download = item.qrcode;
}
a.href = url
a.dispatchEvent(event)
}
image.src = contextPath + item.qrcodeUrl;
}
二、文件下载
/**
* 下载文件
* @param url 文件路径
* @param fileName 文件名
* @param parameter
* @returns {*}
*/
export function downloadFile(url, fileName, parameter) {
return downFile(url, parameter).then((data) => {
if (!data || data.size === 0) {
Vue.prototype['$message'].warning('文件下载失败')
return
}
if (typeof window.navigator.msSaveBlob !== 'undefined') {
window.navigator.msSaveBlob(new Blob([data]), fileName)
} else {
let url = window.URL.createObjectURL(new Blob([data]))
let link = document.createElement('a')
link.style.display = 'none'
link.href = url
link.setAttribute('download', fileName)
document.body.appendChild(link)
link.click()
document.body.removeChild(link) //下载完成移除元素
window.URL.revokeObjectURL(url) //释放掉blob对象
}
})
}
三、文件上传
/**
* 上传
* @param file 文件信息
* @returns {*}
*/
uploadImg:function(file){
let formData = new FormData();
// formData.append("uploadCover", file,this.cropperUrl.name);
formData.append("file", file,this.cropperUrl.name); // 上传文件
formData.append("venueId", this.form.venueId); // 上传参数
let _this=this
this.$http({
url: '/adaptor/adaptor/activity/uploadFile',
method: 'post',
headers: {"Content-Type": "multipart/form-data"},
data:formData,
}).then(function (res) {
if (res.data.RespInfo.errorCode == "00000000") {
console.log(res.data.fileUrl)
}else {
}
}).catch(function (err) {
})
},