diff --git a/src/platforms/h5/helpers/file.js b/src/platforms/h5/helpers/file.js index 3a9a82a6b39611c8b6c3072c4e6c44d6445c23da..c6337cdd552ae047d9d7037442bff986375b2985 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 e3c09b89c46d1fb497405711406438918eaa054d..927b360ecf40034bc6d963c930874375e84e5b11 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 65258be5964f2f03c0edef239b55b86a72ab8ccd..7dc0ccdaee4a90e297100bc0bb7b0db043851eb4 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 {