1

I have a REST Service that returns a Stream of different kinds of files and simple hookup to the Service from a HTML page that prompts for download.

My REST Service looks like the following:

[WebGet(UriTemplate = "downloadfile/{recordId}/{fileType}")]
public Stream DownloadFile(string recordId, string fileType)
{
    string downloadFilePathdoc = "C:\\WORKSPACE\\WIP Code Files\\FileDownloadPOC\\SampleDownloadFileWord.doc";
    String headerInfodoc = "attachment; filename=SampleDownloadFileWord_" + recordId + ".doc";
    WebOperationContext.Current.OutgoingResponse.Headers["Content-Disposition"] = headerInfodoc;
    WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream";
    return File.OpenRead(downloadFilePathdoc);
}

And my HTML page is as follows:

function GetFile(fileType)
{
    var _recId = $("#randomguid").val();
    window.location.href = "http://localhost:6070/RESTFileDownload/Service1.svc/downloadfile/" + _recId + "/" + fileType;
}

Now what I was trying to do is have the method as a POST so I can send it an object as a parameter and still be able to download the file from the client side somehow.

If I do a POST method, I am unable to find a way to prompt download of the file from the browser side. The response comes as the content of the file and I can't figure out how to render that as a file and have it downloaded.

Any help is greatly appreciated.

  • Figured out Ajax calls cannot be used to download files, link : http://stackoverflow.com/questions/12955266/ajax-call-to-download-file-returned-from-restful-service?noredirect=1&lq=1 – EskapeVelocity Nov 08 '16 at 15:07

0 Answers0