Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Download file using WCF Rest service?

Tags:

rest

c#

wcf

If there is a way to Upload file using rest via stream would there be also for "Download"? If yes, can you please tell me how? Thanks in advance!

like image 924
fiberOptics Avatar asked Mar 15 '12 05:03

fiberOptics


People also ask

How do I upload a file using WCF REST service?

Using the Code [DataContract] public class UploadedFile { [DataMember] public string FilePath { get; set; } [DataMember] public string FileLength { get; set; [DataMember] public string FileName { get; set; } //Other information.


1 Answers

A sample method i use to download the file from my REST service:

[WebGet(UriTemplate = "file/{id}")]
        public Stream GetPdfFile(string id)
        {
            WebOperationContext.Current.OutgoingResponse.ContentType = "application/txt";
            FileStream f = new FileStream("C:\\Test.txt", FileMode.Open);
            int length = (int)f.Length;
            WebOperationContext.Current.OutgoingResponse.ContentLength = length;
            byte[] buffer = new byte[length];
            int sum = 0;
            int count;
            while((count = f.Read(buffer, sum , length - sum)) > 0 )
            {
                sum += count;
            }
            f.Close();
            return new MemoryStream(buffer); 
        }
like image 67
Rajesh Avatar answered Sep 28 '22 21:09

Rajesh