Created
April 1, 2020 12:43
-
-
Save reggie3/197491e82ff7d67394ca9e6965e4dfab to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/***************************************** | |
* uploadPicturesToBucketV2 | |
* CURRENT WORKING VERSION | |
* post base64 encoded data to server | |
* This works when used in conjunction with technique from this post: | |
* http://stackoverflow.com/questions/26805005/sending-binary-data-to-amazon-s3-javascript | |
*/ | |
export const uploadPicturesToBucket = (fileArray, userId, jwt, loginType) => { | |
let promiseArray = fileArray.map((file, index, array) => { | |
let splitArray = file.name.split("."); | |
let fileExtension = splitArray[splitArray.length - 1]; | |
return new Promise(function (resolve, reject) { | |
console.log(`uploading to: ${URL}?userId=${userId}&extension=${fileExtension} | |
&version=V2&mimeType=${file.type}`); | |
console.log("uploading file name : " + file.name); | |
// reader technique from here: | |
// https://developer.mozilla.org/en-US/docs/Web/API/FileReader/readAsDataURL | |
var reader = new FileReader(); | |
reader.addEventListener("load", function () { | |
let fileData = reader.result; | |
//; | |
return fetch(URL + `?userId=${userId}&extension=${fileExtension} | |
&version=V2&mimeType=${file.type}`, { | |
method: 'POST', | |
body: reader.result, | |
mode: 'cors', | |
headers: new Headers({ | |
'Content-Type': 'application/json', | |
'Authorization': jwt + "|" + loginType | |
}) | |
}) | |
.then((response) => { | |
return response.json(); | |
}) | |
.then((json) => { | |
console.log(json); | |
/* if (!response.ok) { | |
reject(response.statusText); | |
}*/ | |
if (json.statusCode === "200") { | |
// console.log(json); | |
dropzoneUtilities.setDropzoneFileComplete(file); | |
dropzoneUtilities.removeFile(file); | |
resolve({ | |
name: json.fileName, | |
userId, | |
jwt, | |
loginType | |
}); | |
} | |
else { | |
reject(json.msg); | |
} | |
}) | |
.catch(function (err) { | |
console.log(err); | |
reject(err); | |
}); | |
}, false); | |
reader.readAsDataURL(file); | |
}); | |
}); | |
return Promise.all(promiseArray) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment