title: How do I write Image Binary Data into local file system using Node.js services on webOS?
body: <p>I am trying to write image data received from Evernote <code>getResourceData</code>-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.</p>
  
  <p>The function <code>getResourceData</code> 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.</p>
  
  <ul>
  <li>Hex of original: <a href="http://i41.tinypic.com/ev8bnr.jpg" rel="nofollow">http://i41.tinypic.com/ev8bnr.jpg</a></li>
  <li>Hex of downloaded: <a href="http://i41.tinypic.com/10rs7zm.jpg" rel="nofollow">http://i41.tinypic.com/10rs7zm.jpg</a></li>
  </ul>
  
  <p>Here's the code:</p>
  
  <p><strong>Client.js</strong>:</p>
  
  <pre><code>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 });
  }
  </code></pre>
  
  <p><strong>WriteFileAssistant.js</strong>:</p>
  
  <pre><code>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 };
    });
  }
  </code></pre>
  
  <p>Any help will be greatly appreciated.</p>
  
  <p>-Petro</p>