diff --git a/README.md b/README.md index aee7b83..c8c3f8f 100644 --- a/README.md +++ b/README.md @@ -23,13 +23,15 @@ Call this method to open the Save dialog and store raw contents in a file. The m * file contents as a Blob instance, * optional file name to display on default (the user may change it manually though). +The method returns a promise whose fulfillment value is a URI of the saved file in the user-selected location. + To construct a Blob representation for a file contents, either use the [`Blob` constructor](https://developer.mozilla.org/en-US/docs/Web/API/Blob/Blob) directly: ```javascript let blob = new Blob(["file contents"], {type: "text/plain"}); let fileName = "my-file.txt"; -cordova.plugins.saveDialog.saveFile(blob, fileName).then(() => { - console.info("The file has been successfully saved"); +cordova.plugins.saveDialog.saveFile(blob, fileName).then(uri => { + console.info("The file has been successfully saved to", uri); }).catch(reason => { console.warn(reason); }); @@ -41,9 +43,11 @@ or apply other methods of blob generation (such as [`Response.blob()`](https://d try { let response = await fetch(`https://avatars.dicebear.com/api/avataaars/${Math.random()}.svg`); let blob = await response.blob(); - await cordova.plugins.saveDialog.saveFile(blob, "random-avatar.svg"); - console.info("The file has been successfully saved"); + let uri = await cordova.plugins.saveDialog.saveFile(blob, "random-avatar.svg"); + console.info("The file has been successfully saved to", uri); } catch (e) { console.error(e); } ``` + +Do not try to use the returned URI for getting file access. It is provided for information purposes only. The user stores a file outside the application sandbox, so access to the saved resource is to be considered restricted. diff --git a/package.json b/package.json index abbfff0..59cbf18 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "cordova-plugin-save-dialog", - "version": "1.0.0", + "version": "1.1.0", "description": "Cordova plugin for opening the native Save dialog and storing a file in the user-selected location", "main": "index.js", "repository": { diff --git a/src/android/SaveDialog.java b/src/android/SaveDialog.java index 7c231eb..ef99026 100644 --- a/src/android/SaveDialog.java +++ b/src/android/SaveDialog.java @@ -70,7 +70,7 @@ private void saveFile(Uri uri, String data) { FileOutputStream fileOutputStream = new FileOutputStream(pfd.getFileDescriptor()); try { fileOutputStream.write(rawData); - this.callbackContext.success(); + this.callbackContext.success(uri.toString()); } catch (Exception e) { this.callbackContext.error(e.getMessage()); e.printStackTrace(); diff --git a/src/ios/CDVSaveDialog.m b/src/ios/CDVSaveDialog.m index 11e5676..363aca2 100644 --- a/src/ios/CDVSaveDialog.m +++ b/src/ios/CDVSaveDialog.m @@ -28,7 +28,8 @@ - (void)saveFile:(CDVInvokedUrlCommand*)command - (void)documentPicker:(UIDocumentPickerViewController*)picker didPickDocumentsAtURLs:(NSArray*)urls { if ([urls count] > 0) { - [self sendPluginResult:YES message:nil]; + NSString* url = [[urls objectAtIndex:0] absoluteString]; + [self sendPluginResult:YES message:url]; } else { [self sendPluginResult:NO message:@"Unknown error"]; } @@ -73,7 +74,7 @@ - (void)sendPluginResult:(BOOL)success message:(NSString*)message { CDVPluginResult* pluginResult = nil; if (success) { - pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK]; + pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsString:message]; } else { pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:message]; } diff --git a/www/android/SaveDialog.js b/www/android/SaveDialog.js index 1141045..36c3c61 100644 --- a/www/android/SaveDialog.js +++ b/www/android/SaveDialog.js @@ -26,8 +26,9 @@ module.exports = { return keepBlob(blob) // see the “resume” event handler below .then(() => locateFile(blob.type, name)) .then(uri => saveFile(uri, blob)) - .then(() => { + .then(uri => { clearBlob(); + return uri; }) .catch(reason => { clearBlob();