2

I am trying to write image data received from Evernote getResourceData-method, into local system in JavaScript code using Node.js. The image file is getting saved successfully, but looks like it is corrupted, and I'm not able to open it.

The function getResourceData returns binary data of the resource with the provided GUID. For example, if this were an image resource, this would contain the raw bits of the image.

Here's the code:

Client.js:

var onSuccess = function(resource, transport) {
  self.showResourceData(resource);
  //console.log("Got Resource: "+resource);
};
NoteStore.getResourceData(onSuccess,this.showAlertMessage.bind(this, 
    "Failed to get Resource"), inSender.getValue());

showResourceData: function(resource) {
  //Calls WriteFileAssistant service
  this.$.writeFile.call({ path: "/downloads/logo1.png", content: resource });
}

WriteFileAssistant.js:

WriteFileAssistant.prototype.run = function(future) {
  var filePath = this.controller.args.path;
  var content = this.controller.args.content;

  var downloadfile = fs.createWriteStream(filePath,
      {'flags': 'w', encoding: 'binary'});
  downloadfile.write(content, encoding='binary', function(err) {
    future.result = { path: filePath, bytes: content.length, error: err };
  });
}

Any help will be greatly appreciated.

-Petro

Ben Combee
  • 16,831
  • 6
  • 41
  • 42
Petro
  • 21
  • 4
  • You seem to be using named arguments to `downloadfile.write()` in `WriteFileAssistant.js`. I believe that isn't valid JavaScript. Also, you could shorten your code a bit by using [`fs.writeFile()`](http://nodejs.org/docs/v0.4.0/api/fs.html#fs.writeFile) – Morten Siebuhr Dec 28 '11 at 15:32
  • @Morten Siebuhr, I also tried using fs.writeFile(), as shown below, instead of using last 2 lines in above code. But, still same result. fs.writeFile(filePath, content, encoding='binary', function(err) { future.result = { path: filePath, bytes: content.length, error: err }; }); – Petro Dec 28 '11 at 15:41
  • Sorry to be so dense, but this example doesn't make sense. Where is `this.controller.args.content` being set? What is `this.$.writeFile`? It's the collecting of `content` for `downloadFile.write` that is most likely the issue but you haven't included any of that process here. – Kato Dec 28 '11 at 16:35
  • @Kato, this code is written in Mojo / Enyo Service framework, WriteFileAssistant.js is exposed as a service, `this.$.writeFile` invokes run method of WriteFileAssistant, and while calling this service, we set two parameters, `{ path: "/downloads/logo1.png", content: resource }`... these parameters are accessed in the service as shown below. `var filePath = this.controller.args.path; var content = this.controller.args.content;` – Petro Dec 28 '11 at 16:43
  • Ah, well that makes some sense; let's add a couple tags, shall we? On troubleshooting, I'd still suggest that the generation of `content` is as likely as the write stream to be the culprit. – Kato Dec 28 '11 at 18:21
  • @Kato, yes I also suspect the same, `content` we get from evernote method, `getResourceData`... also, if we look at the HEX images, only few characters are different... but not sure why they are different :( – Petro Dec 29 '11 at 15:37
  • Your path needs to be "/media/internal/downloads" to be able to write a file. There's no write-access to the root of the system filesystem. – Ben Combee Jan 05 '12 at 22:38

0 Answers0