From 0f1ecd18e085b8d8bf2be31f9fd834ce6f18cec5 Mon Sep 17 00:00:00 2001 From: qiang Date: Thu, 3 Dec 2020 21:18:31 +0800 Subject: [PATCH] =?UTF-8?q?fix(h5):=20=E4=BC=98=E5=8C=96=20downloadFile=20?= =?UTF-8?q?=E6=96=87=E4=BB=B6=E5=90=8D=EF=BC=88=E5=BD=93=E7=94=A8=E4=BA=8E?= =?UTF-8?q?=20uploadFile=20=E7=AD=89=E5=85=B6=E4=BB=96=E6=8E=A5=E5=8F=A3?= =?UTF-8?q?=E6=97=B6=E7=94=A8=E5=88=B0=EF=BC=89?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/platforms/h5/helpers/file.js | 10 ++++++++++ .../h5/service/api/network/download-file.js | 17 ++++++++++++++++- 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/platforms/h5/helpers/file.js b/src/platforms/h5/helpers/file.js index 010852ad8f..cfc156331f 100644 --- a/src/platforms/h5/helpers/file.js +++ b/src/platforms/h5/helpers/file.js @@ -55,6 +55,16 @@ function getExtname (type) { const extname = type.split('/')[1] return extname ? `.${extname}` : '' } +/** + * 简易获取文件名 + * @param {*} url + */ +export function getFileName (url) { + url = url.split('#')[0].split('?')[0] + const array = url.split('/') + return array[array.length - 1] +} + /** * blob转File * @param {Blob} blob diff --git a/src/platforms/h5/service/api/network/download-file.js b/src/platforms/h5/service/api/network/download-file.js index cdc2cc0a05..6980157c92 100644 --- a/src/platforms/h5/service/api/network/download-file.js +++ b/src/platforms/h5/service/api/network/download-file.js @@ -1,4 +1,7 @@ -import { fileToUrl } from 'uni-platform/helpers/file' +import { + fileToUrl, + getFileName +} from 'uni-platform/helpers/file' /** * 下载任务 */ @@ -63,6 +66,18 @@ export function downloadFile ({ clearTimeout(timer) const statusCode = xhr.status const blob = this.response + let filename + // 使用 getResponseHeader 跨域时会出现警告,但相比 getAllResponseHeaders 更方便 + const contentDisposition = xhr.getResponseHeader('content-disposition') + if (contentDisposition) { + // 暂时仅解析 filename 不解析 filename* + const res = contentDisposition.match(/filename="?(\S+)"?\b/) + if (res) { + filename = res[1] + } + } + blob.name = filename || getFileName(url) + console.log(blob) invoke(callbackId, { errMsg: 'downloadFile:ok', statusCode, -- GitLab