Skip to content
This repository was archived by the owner on Mar 4, 2025. It is now read-only.

Commit d075e11

Browse files
author
Nick Litwin
committed
Upload to S3
1 parent 7362c77 commit d075e11

File tree

1 file changed

+29
-8
lines changed

1 file changed

+29
-8
lines changed

app/services/submissions.service.js

Lines changed: 29 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -34,18 +34,39 @@
3434
}
3535

3636
function uploadSubmissionFileToS3(filesWithPresignedURL, files) {
37-
var promises = filesWithPresignedURL.map(function(fileWithPresignedURL) {
38-
var S3RequestOptions = {
39-
url: fileWithPresignedURL.preSignedUploadUrl,
40-
method: 'PUT',
41-
// headers: {},
4237

43-
// The file's type is the key, and the value is the actual file to upload
44-
data: files[filesWithPresignedURL.type]
38+
var promises = filesWithPresignedURL.map(function(fileWithPresignedURL) {
39+
var deferred = $q.defer();
40+
var xhr = new XMLHttpRequest();
41+
42+
xhr.open('PUT', fileWithPresignedURL.preSignedUploadUrl, true);
43+
xhr.setRequestHeader('Content-Type', fileWithPresignedURL.mediaType);
44+
45+
// xhr version of the success callback
46+
xhr.onreadystatechange = function() {
47+
var status = xhr.status;
48+
if (((status >= 200 && status < 300) || status === 304) && xhr.readyState === 4) {
49+
$log.info('Successfully uploaded file');
50+
console.log(xhr.responseText);
51+
var xhrResponse = xhr.responseText;
52+
deferred.resolve(xhrResponse);
53+
54+
} else if (status >= 400) {
55+
$log.error('Error uploading to S3 with status: ' + status);
56+
toaster.pop('error', 'Whoops!', 'There was an error uploading your files. Please try again later.');
57+
deferred.reject(err);
58+
}
4559
};
4660

47-
return $http(S3RequestOptions);
61+
xhr.onerror = function(err) {
62+
$log.info('Error uploading to s3');
63+
toaster.pop('error', 'Whoops!', 'There was an error uploading your files. Please try again later.');
64+
deferred.reject(err);
65+
}
66+
67+
xhr.send(files[fileWithPresignedURL.type]);
4868

69+
return deferred.promise;
4970
});
5071

5172
return $q.all(promises)

0 commit comments

Comments
 (0)