Skip to content

Commit

Permalink
Initial stab at implementing progress callbacks for FileTransfer.uplo…
Browse files Browse the repository at this point in the history
…ad on Android
  • Loading branch information
bvibber committed Jul 3, 2012
1 parent 11dbf05 commit c8c1769
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions framework/src/org/apache/cordova/FileTransfer.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ public PluginResult execute(String action, JSONArray args, String callbackId) {
}

if (action.equals("upload")) {
return upload(source, target, args);
return upload(source, target, args, callbackId);
} else if (action.equals("download")) {
return download(source, target);
} else {
Expand All @@ -96,14 +96,15 @@ public PluginResult execute(String action, JSONArray args, String callbackId) {
* @param source Full path of the file on the file system
* @param target URL of the server to receive the file
* @param args JSON Array of args
* @param callbackId callback id for optional progress reports
*
* args[2] fileKey Name of file request parameter
* args[3] fileName File name to be used on server
* args[4] mimeType Describes file content type
* args[5] params key:value pairs of user-defined parameters
* @return FileUploadResult containing result of upload request
*/
private PluginResult upload(String source, String target, JSONArray args) {
private PluginResult upload(String source, String target, JSONArray args, String callbackId) {
Log.d(LOG_TAG, "upload " + source + " to " + target);

HttpURLConnection conn = null;
Expand Down Expand Up @@ -136,6 +137,7 @@ private PluginResult upload(String source, String target, JSONArray args) {
long totalBytes;
byte[] buffer;
int maxBufferSize = 8096;
boolean progress;

//------------------ CLIENT REQUEST
// open a URL connection to the server
Expand Down Expand Up @@ -254,13 +256,28 @@ private PluginResult upload(String source, String target, JSONArray args) {
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
totalBytes = 0;

try {
progress = params.getBoolean("progress");
} catch (JSONException e1) {
progress = false;
}
// -1 indicates in-progress upload
result.setResponseCode(-1);
result.setResponse("");

while (bytesRead > 0) {
totalBytes += bytesRead;
result.setBytesSent(totalBytes);
dos.write(buffer, 0, bufferSize);
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
if (progress) {
Log.d(LOG_TAG, "****** About to send a progress result from upload");
PluginResult progressResult = new PluginResult(PluginResult.Status.OK, result.toJSONObject());
progressResult.setKeepCallback(true);
success(progressResult, callbackId);
}
}

// send multipart form data necesssary after file data...
Expand Down

0 comments on commit c8c1769

Please sign in to comment.