Skip to content

Instantly share code, notes, and snippets.

@codenameone
Created February 22, 2016 03:48
Show Gist options
  • Save codenameone/7f6e07dadbdd169648ed to your computer and use it in GitHub Desktop.
Save codenameone/7f6e07dadbdd169648ed to your computer and use it in GitHub Desktop.
Sample code for uploading to FileStack with the MultipartUpload class from Codename One
public void pictureUpload(final Callback<String> resultURL) {
String picture = Capture.capturePhoto(1024, -1);
if(picture!=null){
String filestack = "https://www.filestackapi.com/api/store/S3?key=MY_KEY&filename=myPicture.jpg";
MultipartRequest request = new MultipartRequest() {
protected void readResponse(InputStream input) throws IOException {
JSONParser jp = new JSONParser();
Map<String, Object> result = jp.parseJSON(new InputStreamReader(input, "UTF-8"));
String url = (String)result.get("url");
if(url == null) {
resultURL.onError(null, null, 1, result.toString());
return;
}
resultURL.onSucess(url);
}
};
request.setUrl(filestack);
try {
request.addData("fileUpload", picture, "image/jpeg");
request.setFilename("fileUpload", "myPicture.jpg");
NetworkManager.getInstance().addToQueue(request);
} catch(IOException err) {
err.printStackTrace();
}
}
}
@codenameone
Copy link
Author

Sample usage of MultipartRequest.

This was taken from this stack overflow question.

From the Codename One project

@codenameone
Copy link
Author

The upload portion of the code above corresponds to this curl request:

curl -X POST -F [email protected] https://www.filestackapi.com/api/store/S3?key=Am3XXBhQ4SxiEQU7cLfM5z
{"url": "https://www.filestackapi.com/api/file/cTy7LpA6SfyG26Wzz7Rx", "size": 124928, "type": "image/jpg", "filename": "filename"}

Taken from https://www.filestack.com/features-upload

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment