Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

writeFile issues in alternate file implementation. #439

Closed
ramonsan opened this issue Aug 16, 2016 · 9 comments
Closed

writeFile issues in alternate file implementation. #439

ramonsan opened this issue Aug 16, 2016 · 9 comments

Comments

@ramonsan
Copy link

I am trying to save a blob in the external memory of device. I was rewriting from old ngCordova code with $cordovaFile and $cordovaFileOpener2 plugins. It correctly worked in Ionic 1 app. Now, with new methods of File implemented by (#283) (6ed32ef) I am trying to do using writeFile in the same app, but seems there is an issue with options parameter:

let storage = cordova.file.externalApplicationStorageDirectory;

  function saveFile(blob, filename) {
    return $cordovaFile.writeFile(storage, filename, blob, true);
  }

But get an error:

Uncaught (in promise) TypeError: Cannot set property 'replace' of undefined(…)
File.writeFile @ ionic.native.js:5015
[...]

OK, maybe boolean is failing, I try passing an option object:

  function saveFile(blob, filename) {
    return $cordovaFile.writeFile(storage, filename, blob, { replace: true });
  }

But another error throw:

Uncaught (in promise) TypeError: Cannot read property 'append' of undefined(…)
(anonymous function) @ ionic.native.js:5029

OK, I will add append to object, but again same error (seems opts is not defined, but should be).

Let's do some hacking. I changed var opts with var opts = {} and try... but:

Uncaught (in promise) FileError {code: 1, message: "NOT_FOUND_ERR"}

Finally, I try create first an empty file, and then overwrite its content:

  function saveFile(blob, props) {
    return $cordovaFile.createFile(storage, filename, true)
      .then(() =>
        $cordovaFile.writeFile(storage, filename, blob, { replace: true, append: true, truncate: 0 })
    );
  }

But got:

Uncaught (in promise) FileError {code: 12, message: "PATH_EXISTS_ERR"}

Some info:

Ionic 1.1.0
Ionic-native: 1.3.16
Cordova: 6.3.1

@ihadeed
Copy link
Collaborator

ihadeed commented Aug 17, 2016

@rapropos can you help us here if you have some time?

@pgg
Copy link

pgg commented Aug 19, 2016

opts variable is declared, but not initialised, so when replaceOrOptions as boolean is passed, the replace property is being set on opts (that does not exist). See line 672 and 675 in https://github.com/driftyco/ionic-native/blob/master/src/plugins/file.ts

@ramonsan
Copy link
Author

ramonsan commented Aug 23, 2016

I am testing the new release with fixes of #468:

let storage = cordova.file.externalApplicationStorageDirectory;

  function saveFile(blob, filename) {
    return $cordovaFile.writeFile(storage, filename, blob, true);
  }

Still returning:

Uncaught (in promise) FileError {code: 1, message: "NOT_FOUND_ERR"}

So, I create an empty file before:

  function saveFile(blob, filename) {
    return $cordovaFile.createFile(storage, filename, true)
      .then(() =>
        $cordovaFile.writeFile(storage, filename, blob, true)
    );
  }

And it works, creates a file and replace it with the content.

Should writeFile create the file if does not exist? Or is this the expected behavior?

@ramonornela
Copy link
Contributor

I have doubts about that too, because I follow the same procedure @rapropos can you help us here if you have some time ;).

@ramonornela
Copy link
Contributor

Related #464

@ihadeed
Copy link
Collaborator

ihadeed commented Oct 12, 2016

Can you try with the latest version v2.2.2 and report back if you still have any issue? Thanks.

@ihadeed ihadeed closed this as completed Oct 12, 2016
@hartherbert
Copy link
Contributor

I'm sorry if this is a stupid question, but why does this don't work ?

let imageSrcData = 'data:image/jpeg;base64,' +base64PictureData;
      let blob = this.b64toBlob(imageSrcData);

      let fileName = this.globalState.username + "-" + Date.now()+".jpg";
      let directory = this.file.dataDirectory+"/takenPictures";

      let fullImagePath = directory + "" + fileName;

      console.log(blob.size/1024 + "kB");
      console.log(directory);
      console.log(fileName);

      let options:WriteOptions = {};

this.file.writeFile(directory,fileName, blob, options).then((res:any)=>{

        console.log("DONE WRITING");
        console.log(res);

        try{
          this.navCtrl.push(EditImagePage,{results:fullImagePath});
        }catch(error){
          alert("setonPictaken Error: "+ error);
        }

      },(error)=>{
        console.error("Error while saving image");
        console.log(error);
      });

the blob object is fine, the path and the filename are fine. But this function won't call success or error. it just does nothing. Even if I set the WriteOptions it don't work. can somebody help me ?

6.5.0
Ionic Framework Version: 3.0.1
Ionic CLI Version: 2.2.2
Ionic App Lib Version: 2.2.1
Ionic App Scripts Version: 1.3.1
ios-deploy version: Not installed
ios-sim version: Not installed
OS: Windows 7
Node Version: v6.9.1
Xcode version: Not installed

@danilofd
Copy link

@hartherbert same problem this function won't call success or error

@y-ahlers
Copy link

I little time passed since last comment, but this.file.dataDirectory already comes with a '/'. So you could try let directory = this.file.dataDirectory+"takenPictures";. Might be the problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants