预览
封装js文件
import axios from 'axios';
export async function previewImage(fileAddress) {
const res = await axios({
method: 'post',
data: { downloadUrl: fileAddress },
headers: { Authorization: 'Bearer ' + getToken() },
url: 'xxxxxx',
responseType: 'blob',
});
return new Promise((resolve, reject) => {
if (res && res.data && res.data.size) {
let blob = new Blob([res.data], { type: 'image/jpeg' });
const imageUrl = URL.createObjectURL(blob);
resolve(imageUrl);
}
});
}
页面调用
import { previewImage } from '@/utils/index';
link(row) {
previewImage(row.fileAddress).then((res) => {
if (res) {
this.dialogVisible = true;
this.dialogImageUrl = res;
}
})
},
下载
export function downloadurl(fileAddress, fileName) {
OBSUploadDownload({ downloadUrl: fileAddress }).then((res) => {
const content = res;
const blob = new Blob([content], {
type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
});
if ('download' in document.createElement('a')) {
const elink = document.createElement('a');
elink.download = fileName;
elink.style.display = 'none';
elink.href = URL.createObjectURL(blob);
document.body.appendChild(elink);
elink.click();
URL.revokeObjectURL(elink.href);
document.body.removeChild(elink);
} else {
navigator.msSaveBlob(blob, fileName);
}
});
}
页面调用
import { downloadurl } from '@/utils/downloadFile';
downloadFile(row) {
downloadurl(row.fileAddress, row.fileName);
},