Download file using WCF REST services
Hi,
Generally we used to download a file using a handlers and sending parameters as a querystring (lets say download.ashx). Here few cases we might need to impersonate the user also. It purely depends on environment.
Here is a sample code snippet just for reference.
But instead of that we can download a file as a stream using a WCF REST service too.
Here is the code snippet for declaration in interface ..
and here is the code for Implementation part.
Generally we used to download a file using a handlers and sending parameters as a querystring (lets say download.ashx). Here few cases we might need to impersonate the user also. It purely depends on environment.
Here is a sample code snippet just for reference.
if (extension.Trim() == ".pdf") { context.Response.Clear(); context.Response.ContentType = "application/pdf"; context.Response.AddHeader("content-disposition", "attachment;filename=" + queryFile + ".pdf"); context.Response.BinaryWrite(System.IO.File.ReadAllBytes(file)); }
But instead of that we can download a file as a stream using a WCF REST service too.
Here is the code snippet for declaration in interface ..
/// <summary> /// To fetch documents /// </summary> /// <param name="filePath">Represent text of filepath</param> /// <param name="fileName">Represents text of filename</param> /// <returns>Fetch documents</returns> [OperationContract] [WebGet(UriTemplate = "DownloadFile/{filePath}/{fileName}")] Stream DownloadFile(string filePath, string fileName);
and here is the code for Implementation part.
/// <summary> /// To fetch documents /// </summary> /// <param name="filePath">Represent text of filepath</param> /// <param name="fileName">Represents text of filename</param> /// <returns>Fetch documents</returns> public Stream DownloadFile(string filePath, string fileName) { string downloadFilePath = Path.Combine(HostingEnvironment.MapPath("~/FOLDERPATH/"), filePath + @"/" + fileName); WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream"; return File.OpenRead(downloadFilePath); }Regards, Pavan N
Comments
Post a Comment