|
34 | 34 | }
|
35 | 35 |
|
36 | 36 | function uploadSubmissionFileToS3(filesWithPresignedURL, files) {
|
37 |
| - var promises = filesWithPresignedURL.map(function(fileWithPresignedURL) { |
38 |
| - var S3RequestOptions = { |
39 |
| - url: fileWithPresignedURL.preSignedUploadUrl, |
40 |
| - method: 'PUT', |
41 |
| - // headers: {}, |
42 | 37 |
|
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 | + } |
45 | 59 | };
|
46 | 60 |
|
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]); |
48 | 68 |
|
| 69 | + return deferred.promise; |
49 | 70 | });
|
50 | 71 |
|
51 | 72 | return $q.all(promises)
|
|
0 commit comments