Skip to main content
Participant
October 28, 2008
Question

Read Write Problems, Javascript

  • October 28, 2008
  • 4 replies
  • 411 views
Hello,

I have had problems with a simple procedure. The grand scheme involves a lot more, so my example may seem trivial but this is where I've narrowed the problem down to. I cannot read an image file using the File object and write it back out as an image. Here is my test function, my targetengine for the script is session. When using the BINARY encoding the replicated file is the same size and appears almost identical but it simply won't be recognized as a valid jpg

function readWrite(){
var name = "/c/indesign_scripts/Winter.jpg";
var f = new File(name);
var encoding = "BINARY";
if(!File.isEncodingAvailable(encoding))
alert("You're using an unsupported encoding");

f.encoding = encoding;
f.open("r");
var payload = f.read();

var f2 = new File("/c/indesign_scripts/Winter-1.jpg");
f2.open("w");
var bool = f2.write(payload);
if(!bool)
alert(f2.error);
//*/

}

Thanks in advance for any advice,
Matt
This topic has been closed for replies.

4 replies

Participant
October 28, 2008
Hey Peter,

Thanks a lot for the advice, I believe the flaw was that I wasn't closing the file objects after finishing the process, which lead to the behavior I observed.

Thanks again,
Matt
Peter Kahrel
Community Expert
Community Expert
October 28, 2008
This works for me:

infile = File ("/c/indesign_scripts/Winter.jpg");
infile.encoding = "BINARY";

outfile = new File ("/c/indesign_scripts/Winter-1.jpg");
outfile.encoding = "BINARY";

infile.open ("r", undefined, undefined);
outfile.open ("w", undefined, undefined);

p = infile.read();
outfile.write (p);
outfile.close();

Peter
Participant
October 28, 2008
Good thinking but no luck, I've tried various ways to export it from InDesign and each time I still get a corrupt image. I'm wondering if somehow InDesign's File object isn't properly reading the image file, I've tested it with GIFs and JPGs so far and each has come out corrupted.
Peter Kahrel
Community Expert
Community Expert
October 28, 2008
You should probably set the encoding of f2 to binary as well.

Peter