0

I'm trying to save a JPG file using appjs.. The code I use is from the sample here. As you can see, I simply passed the fileContent into nodejs's writeFile(). It works, but the resulting JPG image doesn't open (IrfanView shows the error "Not a JPEG file: starts with 0xc3 0xbf". Am I missing something? encoding?

Here's the full code I'm using for the drag and drop and save thing. Pretty straightforward:-

addEventListener('app-ready', function(e){
  var fs   = require('fs');

var readEnd = function(progressEvent) {
    console.log('readEnd',progressEvent,this);
    var fileReader = this;
    var fileContent = fileReader.result;
    var fileName = fileReader.file.name;
    // Note you can not retreive file path, for security reasons. 
    // But you are not supposed to need it, you already have the content ;)
    fs.writeFile("test.jpg", fileContent, function (err) {
      if (err) throw err;
      console.log('It\'s saved!');
    });
    console.log('readEnd:');
}


var readFile = function(file) {
    var reader = new FileReader();
    reader.file = file; // We need it later (filename)
    reader.addEventListener('loadend', readEnd, false);
    reader.readAsBinaryString(file);
}

var drop = function(event) {
    event.preventDefault();
    var dt = event.dataTransfer;
    var files = dt.files;
    for (var i = 0; i<files.length; i++) {
        var file = files[i];
        readFile(file);
    }
}

window.addEventListener("drop", drop);
  window.dispatchEvent(new Event('app-done'));
});

Here is the ZIP file containing the original JPG and the resulting JPG : http://ge.tt/4iYy9Kk/v/0?c

Irfan
  • 1,758
  • 3
  • 24
  • 32
  • Please give an example of the code that *you* are using to save the file, not the sample. – nanofarad Jun 26 '13 at 16:01
  • And put resulting JPG somewhere, where we can download it and have a look. – David Jashi Jun 26 '13 at 16:02
  • @ObsessiveSSOℲ Done. JPGs also added. – Irfan Jun 26 '13 at 16:32
  • Why do you require 'fs' only on event? – raam86 Jun 26 '13 at 16:33
  • @raam86 Because this is run inside appjs and 'require' is undefined when I try to call outside the event. – Irfan Jun 26 '13 at 16:47
  • @Power-Inside you never require appjs though... – raam86 Jun 26 '13 at 17:18
  • Try echoing out fileContent to the console using console.log(fileContent); is it what you expect it is -- i.e. the same as looking directly at the JPEG on the disk? – sihorton Jul 02 '13 at 10:34
  • @sihorton Doing that shows large gibberish.. so fileContent is indeed having something in it. – Irfan Jul 02 '13 at 13:40
  • ok so I guess that means that the problem is actually in reading the dropped file from disk, and that is probably an encoding issue. I don't know the API that well but maybe the readAsBinaryString function is not really giving you the binary of the jpeg but is changing the encoding so you will need to decode it afterwards. See if there are other methods to read the file and then experiment until you can get out what is actually on disk. – sihorton Jul 08 '13 at 20:23

0 Answers0