|
@@ -33,18 +33,24 @@ export default {
|
|
|
if (res.code === 0) {
|
|
|
that.name = res.data.name;
|
|
|
that.downUrl = res.data.fileUrl;
|
|
|
- that.downloadFile();
|
|
|
+ that.downloadFile(that.name, that.downUrl);
|
|
|
}
|
|
|
});
|
|
|
},
|
|
|
|
|
|
- async downloadFile() {
|
|
|
+ async downloadFile(name, downUrl) {
|
|
|
var that = this;
|
|
|
+ var fileName = that.getFileExtension(downUrl);
|
|
|
+ var finalName = "";
|
|
|
+ if (name != null && name != "") {
|
|
|
+ finalName = name + "." + fileName;
|
|
|
+ } else {
|
|
|
+ finalName = new Date().getTime() + "." + fileName;
|
|
|
+ }
|
|
|
+
|
|
|
try {
|
|
|
// https://music-play.oss-cn-shenzhen.aliyuncs.com/backOss/file/f5f5ed93fae74fb683e8d116c40a884c.xlsx
|
|
|
- const fileUrl = that.downUrl; // 替换为你的文件路径
|
|
|
const response = await fetch(fileUrl);
|
|
|
-
|
|
|
if (!response.ok) {
|
|
|
throw new Error(`HTTP error! status: ${response.status}`);
|
|
|
}
|
|
@@ -54,22 +60,17 @@ export default {
|
|
|
const a = document.createElement("a");
|
|
|
a.style.display = "none";
|
|
|
a.href = url;
|
|
|
- var fileName = that.getFileExtension(fileUrl);
|
|
|
- const name = that.name;
|
|
|
- if (name != null && name != "") {
|
|
|
- a.download = name + "." + fileName;
|
|
|
- } else {
|
|
|
- a.download = new Date().getTime() + "." + fileName;
|
|
|
- }
|
|
|
+ a.download = finalName;
|
|
|
document.body.appendChild(a);
|
|
|
a.click();
|
|
|
window.URL.revokeObjectURL(url);
|
|
|
} catch (error) {}
|
|
|
},
|
|
|
|
|
|
- getFileExtension(fileUrl) {
|
|
|
- const filename = fileUrl.split("/").pop();
|
|
|
- const lastDotIndex = filename.lastIndexOf(".");
|
|
|
+ ///截取文件
|
|
|
+ getFileExtension(downUrl) {
|
|
|
+ var filename = downUrl.split("/").pop();
|
|
|
+ var lastDotIndex = filename.lastIndexOf(".");
|
|
|
if (lastDotIndex === -1 || lastDotIndex === filename.length - 1) {
|
|
|
return "";
|
|
|
} else {
|