Downloading a binary file through an IS service

Hello -

I’m trying build a webMethods service that returns a binary file for download. The built-in WmPublic service pub.flow:setResponse service only works for a string response.

After some headache, I got the following Java service going - it sets the appropriate headers that prompt the user to download a binary file, and uses Service.setResponse() to set a binary HTTP response.

However, setResponse requires a Values object. The only way I could get a Values object from the pipeline here was to use Values.use() which is deprecated. Does anyone know a better way to do this?

Regards,
Sonam

// pipeline
IDataCursor pipelineCursor = pipeline.getCursor();
	byte[]	responseBytes = (byte []) IDataUtil.get( pipelineCursor, "responseBytes" );
pipelineCursor.destroy();


//Set HTTP Response for client to do Binary Download 
com.wm.net.HttpHeader h = Service.getHttpResponseHeader(null); 
h.setResponse(200, "OK"); 
h.addField("Content-type", "application/binary");
h.addField("Content-disposition", "attachment; filename=somefile.der");
//Note, Values.use is deprecated 
Values in = Values.use(pipeline);
Service.setResponse(in, responseBytes);

Here’s code I’ve used in the past:
[highlight=java]
setResponseBytes(IData pipeline)
IDataCursor idc = pipeline.getCursor();
byte bytes = (byte )IDataUtil.get(idc, “bytes”);
String encoding = IDataUtil.getString(idc, “encoding”);
String contentType = IDataUtil.getString(idc, “contentType”);
String remoteFilename = IDataUtil.getString(idc, “remoteFilename”);
idc.destroy();

com.wm.net.HttpHeader httpheader = Service.getHttpResponseHeader();

if(httpheader != null)
{
if(contentType!=null && !contentType.equals(“”))
httpheader.addField(“content-type”, contentType);
if(encoding!=null && !encoding.equals(“”))
httpheader.addField(“encoding”,encoding);
if(remoteFilename!=null && !remoteFilename.equals(“”))
httpheader.addField(“content-disposition”, “inline;filename= "”+ remoteFilename+ “"”);
}
Service.setResponse(bytes);
[/highlight]

Thanks Rob - that was a quick reply!

The key difference was the last line of your code:
Service.setResponse(bytes);

The webMethods Java API for ‘Service’ documents only one setResponse method:

Is using ‘setResponse(bytes)’ without the Values object safe?

I don’t recall where I found that method reference but clearly it is undocumented and it is possible (though unlikely) that it could go away.

Thanks Rob!

On another note, I put up a thread with info on HTTP uploading binary data into IS:
“HTTP uploading a binary file to IS”
[url]wmusers.com