locked
WCF Rest Download Stream and Open Excel File using Javascript RRS feed

  • Question

  • User2130616632 posted

    Im trying to open an excel file using the stream that i have downloded on wcf service. the problem is when i download the file, it shows on the browser. i was expecting that you will be able to dowload a physical file. im using pure javascript and wcf service. I know it can be done using .ashx. my goal is to use wcf service

    public Stream DownloadFile(string fileName, string fileExtension)
            {
                string downloadFilePath = Path.Combine(HostingEnvironment.MapPath("~/FileServer"), fileName + "." + fileExtension);
                WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream";
                return File.OpenRead(downloadFilePath);
            }
     [OperationContract]
            [WebGet(UriTemplate = "File/{fileName}/{fileExtension}")]
            Stream DownloadFile(string fileName, string fileExtension


    and call the url path eg. http://localhost/File/DownloadFile/filename/xls

    Wednesday, January 8, 2014 9:50 PM

Answers

  • User-1408041064 posted

    Try definging Content-Disposition header also, so the client's browser should automatically begin downloading the file with a given filename.

    e.g. Add below bold bits:

    public Stream DownloadFile(string fileName, string fileExtension)
           
    {
               
    string downloadFilePath = Path.Combine(HostingEnvironment.MapPath("~/FileServer"), fileName + "." + fileExtension);

                String headerInfo = "attachment; filename=" + fileName + "." + fileExtension;
                WebOperationContext.Current.OutgoingResponse.Headers["Content-Disposition"] = headerInfo;


               
    WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream";
               
    return File.OpenRead(downloadFilePath);
           
    }

    Regards

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 9, 2014 6:19 AM

All replies

  • User-1408041064 posted

    Try definging Content-Disposition header also, so the client's browser should automatically begin downloading the file with a given filename.

    e.g. Add below bold bits:

    public Stream DownloadFile(string fileName, string fileExtension)
           
    {
               
    string downloadFilePath = Path.Combine(HostingEnvironment.MapPath("~/FileServer"), fileName + "." + fileExtension);

                String headerInfo = "attachment; filename=" + fileName + "." + fileExtension;
                WebOperationContext.Current.OutgoingResponse.Headers["Content-Disposition"] = headerInfo;


               
    WebOperationContext.Current.OutgoingResponse.ContentType = "application/octet-stream";
               
    return File.OpenRead(downloadFilePath);
           
    }

    Regards

     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, January 9, 2014 6:19 AM
  • User2130616632 posted

    thanks a lot!

    Thursday, January 9, 2014 8:13 PM