JQuery to get file from a webservice

Note: I have moved to blog at my own domain, so kindly visit this post over there for recent updates or comments. http://www.imranbalouch.com/blog/index.php/2011/07/jquery-to-get-file-from-a-webservice/

Recently we were working on a project, which was ASP.NET 3.0 based and used jquery to get and set data. Our design was to have Presentation layer at separate place and business layer at separate place, so we introduced webservices in it.

We had a use case in which user has to download file, either PDF or ZIP file. In traditional server side code we use to put the byte array of file in Response and user get a download option but in jquery we got stuck as byte array wasn’t getting handled by jquery.

The soultion that we implemented was that we created a server side static method with return type string in our ASP.NET and marked it as a WebMethod.

public static string DownloadFile(Int64[] requestId)

if input parameter array has one value, system returns PDF otherwise ZIP file.
Our server side gets data from the web service and writes the file in a folder and return the file virtual path like http://localhost/myapp/tempfolder/myfile.zip to the calling jquery method.

jquery method than uses that url by calling window.open method and user gets that file downloaded.

I feel like that its not a better way of doing it but still don’t have any other.
I will be glad to have a better suggestion from anyone.

All is Well!