From 9e2de20d50a55d4336a147521b184904b41246f3 Mon Sep 17 00:00:00 2001 From: qiang Date: Fri, 3 Apr 2020 13:43:01 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20H5=E7=AB=AF=E9=80=89=E6=8B=A9=E6=96=87?= =?UTF-8?q?=E4=BB=B6=E6=8E=A5=E5=8F=A3=E6=94=AF=E6=8C=81=E8=BF=94=E5=9B=9E?= =?UTF-8?q?=E5=8E=9F=E5=A7=8BFile=E5=AF=B9=E8=B1=A1=20=20close=20#1510=20#?= =?UTF-8?q?1004?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/platforms/h5/helpers/file.js | 5 ++++ .../h5/service/api/media/choose-image.js | 25 ++++++++--------- .../h5/service/api/media/choose-video.js | 27 +++++++++++-------- 3 files changed, 34 insertions(+), 23 deletions(-) diff --git a/src/platforms/h5/helpers/file.js b/src/platforms/h5/helpers/file.js index 3a9a82a6..c6337cdd 100644 --- a/src/platforms/h5/helpers/file.js +++ b/src/platforms/h5/helpers/file.js @@ -61,3 +61,8 @@ export function fileToUrl (file) { files[url] = file return url } + +export function revokeObjectURL (url) { + (window.URL || window.webkitURL).revokeObjectURL(url) + delete files[url] +} diff --git a/src/platforms/h5/service/api/media/choose-image.js b/src/platforms/h5/service/api/media/choose-image.js index e3c09b89..927b360e 100644 --- a/src/platforms/h5/service/api/media/choose-image.js +++ b/src/platforms/h5/service/api/media/choose-image.js @@ -50,26 +50,27 @@ export function chooseImage ({ document.body.appendChild(imageInput) imageInput.addEventListener('change', function (event) { - const tempFilePaths = [] const tempFiles = [] const fileCount = event.target.files.length for (let i = 0; i < fileCount; i++) { const file = event.target.files[i] - const filePath = fileToUrl(file) - - tempFilePaths.push(filePath) - tempFiles.push({ - path: filePath, - size: file.size, - name: file.name + let filePath + Object.defineProperty(file, 'filePath', { + get () { + filePath = filePath || fileToUrl(file) + return filePath + } }) + tempFiles.push(file) } - - invoke(callbackId, { + const res = { errMsg: 'chooseImage:ok', - tempFilePaths: tempFilePaths, + get tempFilePaths () { + return tempFiles.map(({ filePath }) => filePath) + }, tempFiles: tempFiles - }) + } + invoke(callbackId, res) // TODO 用户取消选择时,触发 fail,目前尚未找到合适的方法。 }) diff --git a/src/platforms/h5/service/api/media/choose-video.js b/src/platforms/h5/service/api/media/choose-video.js index 65258be5..7dc0ccda 100644 --- a/src/platforms/h5/service/api/media/choose-video.js +++ b/src/platforms/h5/service/api/media/choose-video.js @@ -1,4 +1,4 @@ -import { fileToUrl } from 'uni-platform/helpers/file' +import { fileToUrl, revokeObjectURL } from 'uni-platform/helpers/file' import { updateElementStyle } from 'uni-shared' const { @@ -42,23 +42,30 @@ export function chooseVideo ({ videoInput.addEventListener('change', function (event) { const file = event.target.files[0] - const filePath = fileToUrl(file) - - let callbackResult = { + const callbackResult = { errMsg: 'chooseVideo:ok', - tempFilePath: filePath, + tempFile: file, size: file.size, duration: 0, width: 0, height: 0, name: file.name } + let filePath + Object.defineProperty(callbackResult, 'tempFilePath', { + get () { + filePath = filePath || fileToUrl(this.tempFile) + return filePath + } + }) const video = document.createElement('video') if (video.onloadedmetadata !== undefined) { + const filePath = fileToUrl(file) // 尝试获取视频的宽高信息 video.onloadedmetadata = function () { - invoke(callbackId, Object.assign({}, callbackResult, { + revokeObjectURL(filePath) + invoke(callbackId, Object.assign(callbackResult, { duration: video.duration || 0, width: video.videoWidth || 0, height: video.videoHeight || 0 @@ -66,11 +73,9 @@ export function chooseVideo ({ } // 部分浏览器(如微信内置浏览器)未播放无法触发loadedmetadata事件 setTimeout(() => { - invoke(callbackId, Object.assign({}, callbackResult, { - duration: 0, - width: 0, - height: 0 - })) + video.onloadedmetadata = null + revokeObjectURL(filePath) + invoke(callbackId, callbackResult) }, 300) video.src = filePath } else { -- GitLab