From fec174127588b0c7a4184a7eb6014fcb7bc35faf Mon Sep 17 00:00:00 2001 From: Diego Mello Date: Wed, 23 Dec 2020 08:49:00 -0300 Subject: [PATCH] [REGRESSION] Multiple uploads not working on iOS (#2738) * Update React Native to 0.63.4 * Fix multiple uploads not working on iOS --- app/utils/fileUpload/index.ios.js | 58 +++++++++++++++++-------------- 1 file changed, 31 insertions(+), 27 deletions(-) diff --git a/app/utils/fileUpload/index.ios.js b/app/utils/fileUpload/index.ios.js index 81a94d879..9b8d8d08e 100644 --- a/app/utils/fileUpload/index.ios.js +++ b/app/utils/fileUpload/index.ios.js @@ -1,46 +1,50 @@ +class Upload { + constructor() { + this.xhr = new XMLHttpRequest(); + this.formData = new FormData(); + } + + then = (callback) => { + this.xhr.onload = () => callback({ respInfo: this.xhr }); + this.xhr.send(this.formData); + } + + catch = (callback) => { + this.xhr.onerror = callback; + } + + uploadProgress = (callback) => { + this.xhr.upload.onprogress = ({ total, loaded }) => callback(loaded, total); + } + + cancel = () => { + this.xhr.abort(); + return Promise.resolve(); + } +} + class FileUpload { - _xhr = new XMLHttpRequest(); - - _formData = new FormData(); - fetch = (method, url, headers, data) => { - this._xhr.open(method, url); + const upload = new Upload(); + upload.xhr.open(method, url); Object.keys(headers).forEach((key) => { - this._xhr.setRequestHeader(key, headers[key]); + upload.xhr.setRequestHeader(key, headers[key]); }); data.forEach((item) => { if (item.uri) { - this._formData.append(item.name, { + upload.formData.append(item.name, { uri: item.uri, type: item.type, name: item.filename }); } else { - this._formData.append(item.name, item.data); + upload.formData.append(item.name, item.data); } }); - return this; - } - - then = (callback) => { - this._xhr.onload = () => callback({ respInfo: this._xhr }); - this._xhr.send(this._formData); - } - - catch = (callback) => { - this._xhr.onerror = callback; - } - - uploadProgress = (callback) => { - this._xhr.upload.onprogress = ({ total, loaded }) => callback(loaded, total); - } - - cancel = () => { - this._xhr.abort(); - return Promise.resolve(); + return upload; } }