I’ve been working on a web service to return assort XML data, but know I wish to return a pdf file with a new method. How do I return the file as the result and not a link to the file?
Thanks all. Just getting used to 6 and all the new toys. Crash building a web service is one of them
Can you return a byte array with the file content and save it in local?
Best regards.
So how would you return a byte array then as part of the xml stream? Not sure I understand. I have tried writing to byte array, but nothing seems to return. When I was working in Developer I got a save file dialog. On a server nada that I could see.
Something along the lines of:
respondXML[new Object(‘file’)] = plugins.file.writeFile(plugins.file.readFile(fileNameis), bytes,‘application/pdf’)
Or use Velocity’s new web capabilities to do this: plugins.Velocity.createByteResponse(bytes, mimeType) ![Wink ;)]()
Actually to embed in XML, you will have to convert to Base64. XML is not a binary format!
It’s also possible to save the file on the server somewhere in /application_server/server/webapps/ROOT/ in a subfolder subfolder and return a URL to that file (which will be something like: http://yourserverURL/subfolder/yourPDF.pdf)
If the pdf is in the server, read it in a byte array.
var bytes = plugins.file.readFile('big.jpg');
The return bytes in DATA section of XML
var myXML = "....";
myXML+="<fileName>myFile.pdf</fileName>"
myXML+="<fileContent><![CDATA[" + bytes + "]]></fileContent>"
myXML+="..."
And in local side save “bytes” as a file:
var f = plugins.file.convertToJSFile('myFile.pdf');
plugins.file.writeFile(f, bytes)
myXML+="<fileContent><![CDATA[" + bytes + "]]></fileContent>"
will never work!
Because you take binary data and you concatenate to a String, so any non character bytes will be skipped and you will get garbage result…
You need to encode the byte array using Base64 encoding.
You can use Packages.org.apache.commons.codec.binary.Base64.encodeBase64(bytes) that will return a properly encoded String that you can then put into you CDATA section.
And on the receiving side, you will need to decode that Base64 String or course.
Really; I need convert to base64. Just missing the most important 
Excellent help. Thank you. I think I may be on the right track now. I have it encoded and am testing the whole process today. I’ll try to post the result when complete.
This worked pretty well. Had some problems with getting the wholebyte array into cdata. And ended up at the clients request moving to just returning a link to the file. Which does cause two gets, but seems to be working better for them at the moment.
That’s 2 request but the XML is much lighter so it shouldn’t be a problem anyway (Bas64 encoding will make your data a lot bigger).
I think that’s the best way to go.