-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #16 from protegeproject/reimplement-multi-part-fil…
…e-upload Reimplement multi part file upload
- Loading branch information
Showing
5 changed files
with
120 additions
and
63 deletions.
There are no files selected for viewing
This file contains 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
11 changes: 11 additions & 0 deletions
11
...ent/src/main/java/edu/stanford/bmir/protege/web/client/upload/FileUploadErrorHandler.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package edu.stanford.bmir.protege.web.client.upload; | ||
|
||
/** | ||
* Matthew Horridge | ||
* Stanford Center for Biomedical Informatics Research | ||
* 2024-06-10 | ||
*/ | ||
public interface FileUploadErrorHandler { | ||
|
||
void handleFileUploadError(int statusCode); | ||
} |
11 changes: 11 additions & 0 deletions
11
...t/src/main/java/edu/stanford/bmir/protege/web/client/upload/FileUploadSuccessHandler.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package edu.stanford.bmir.protege.web.client.upload; | ||
|
||
/** | ||
* Matthew Horridge | ||
* Stanford Center for Biomedical Informatics Research | ||
* 2024-06-10 | ||
*/ | ||
public interface FileUploadSuccessHandler { | ||
|
||
void handleFileUploadSuccess(String fileSubmissionId); | ||
} |
56 changes: 56 additions & 0 deletions
56
...gwt-ui-client/src/main/java/edu/stanford/bmir/protege/web/client/upload/FileUploader.java
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package edu.stanford.bmir.protege.web.client.upload; | ||
|
||
/** | ||
* Matthew Horridge | ||
* Stanford Center for Biomedical Informatics Research | ||
* 2024-06-10 | ||
*/ | ||
public class FileUploader { | ||
|
||
private static final String END_POINT = "/files/submit"; | ||
|
||
/** | ||
* Upload a file that is retrieved from a FileInput element | ||
* @param fileInputId The Id of the file input element | ||
* @param token The authentication token to use when uploading the file | ||
* @param successHandler The handler that will be called if the upload succeeds. | ||
* @param errorHandler The handle that will be called if the upload fails. | ||
*/ | ||
public void uploadFile(String fileInputId, | ||
String token, | ||
FileUploadSuccessHandler successHandler, | ||
FileUploadErrorHandler errorHandler) { | ||
uploadFileNative(fileInputId, END_POINT, token, successHandler, errorHandler); | ||
} | ||
|
||
private native void uploadFileNative(String fileInputId, String endPoint, String token, | ||
FileUploadSuccessHandler successHandler, | ||
FileUploadErrorHandler errorHandler)/*-{ | ||
var fileElement = $doc.getElementById(fileInputId); | ||
var file = fileElement.files[0]; | ||
var formData = new FormData(); | ||
formData.append("file", file); | ||
var xhr = new XMLHttpRequest(); | ||
xhr.open("POST", endPoint, true); | ||
var authHeader = "bearer " + token; | ||
xhr.setRequestHeader('Authorization', authHeader); | ||
xhr.onreadystatechange = function () { | ||
if (xhr.readyState === 4) { | ||
if(xhr.status === 200) { | ||
var submissionId = xhr.responseText.substring(1, xhr.responseText.length - 1); | ||
successHandler.@edu.stanford.bmir.protege.web.client.upload.FileUploadSuccessHandler::handleFileUploadSuccess(*)(submissionId); | ||
} | ||
else { | ||
var errorCode = xhr.status; | ||
errorHandler.@edu.stanford.bmir.protege.web.client.upload.FileUploadErrorHandler::handleFileUploadError(*)(errorCode); | ||
} | ||
} | ||
}; | ||
// Initiate a multipart/form-data upload | ||
xhr.send(formData); | ||
}-*/; | ||
} |
This file contains 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