Skip to content

Commit

Permalink
Provide saved file’s URI as a promise fulfillment value
Browse files Browse the repository at this point in the history
  • Loading branch information
Amphiluke committed Aug 18, 2022
1 parent c92a275 commit 6920cea
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
12 changes: 8 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
Expand All @@ -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.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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": {
Expand Down
2 changes: 1 addition & 1 deletion src/android/SaveDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 3 additions & 2 deletions src/ios/CDVSaveDialog.m
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ - (void)saveFile:(CDVInvokedUrlCommand*)command
- (void)documentPicker:(UIDocumentPickerViewController*)picker didPickDocumentsAtURLs:(NSArray<NSURL*>*)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"];
}
Expand Down Expand Up @@ -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];
}
Expand Down
3 changes: 2 additions & 1 deletion www/android/SaveDialog.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 6920cea

Please sign in to comment.