diff --git a/src/platforms/h5/helpers/file.js b/src/platforms/h5/helpers/file.js index 84ebaf6946abed22823208f1a06f848bc2578bd6..861ed8307927d8544cd623e9660e04aca8d2cf34 100644 --- a/src/platforms/h5/helpers/file.js +++ b/src/platforms/h5/helpers/file.js @@ -44,8 +44,35 @@ export function base64ToFile (base64) { while (n--) { array[n] = str.charCodeAt(n) } - var filename = `${Date.now()}.${type.split('/')[1]}` - return new File([array], filename, { type: type }) + return blobToFile(array, type) +} +/** + * 简易获取扩展名 + * @param {string} type + * @return {string} + */ +function getExtname (type) { + const extname = type.split('/')[1] + return extname ? `.${extname}` : '' +} +/** + * blob转File + * @param {Blob} blob + * @param {string} type + * @return {File} + */ +export function blobToFile (blob, type) { + if (!(blob instanceof File)) { + type = type || blob.type || '' + const filename = `${Date.now()}${getExtname(type)}` + try { + blob = new File([blob], filename, { type }) + } catch (error) { + blob = blob instanceof Blob ? blob : new Blob([blob], { type }) + blob.name = blob.name || filename + } + } + return blob } /** * 从本地file或者blob对象创建url diff --git a/src/platforms/h5/service/api/network/upload-file.js b/src/platforms/h5/service/api/network/upload-file.js index f160fb20355287ee0da7b2197a221052c5c9f0e2..d75859087ef1bf4bba19459cb880313367a7d686 100644 --- a/src/platforms/h5/service/api/network/upload-file.js +++ b/src/platforms/h5/service/api/network/upload-file.js @@ -1,4 +1,7 @@ -import { urlToFile } from 'uni-platform/helpers/file' +import { + urlToFile, + blobToFile +} from 'uni-platform/helpers/file' /** * 上传任务 */ @@ -133,7 +136,7 @@ export function uploadFile ({ } Promise - .all(files.map(({ file, uri }) => file instanceof Blob ? Promise.resolve(file) : urlToFile(uri))) + .all(files.map(({ file, uri }) => file instanceof Blob ? Promise.resolve(blobToFile(file)) : urlToFile(uri))) .then(upload) .catch(() => { setTimeout(() => {