Reputation: 7165
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!
Upvotes: 7
Views: 17160
Reputation: 4253
You can also use the following
public Stream GetFile(string id)
{
WebOperationContext.Current.OutgoingResponse.ContentType = "application/txt";
var byt = File.ReadAllBytes("C:\\Test.txt");
WebOperationContext.Current.OutgoingResponse.ContentLength = byt.Length;
return new MemoryStream(byt);
}
when it's defined as
[WebGet(UriTemplate = "file/{id}")]
[OperationContract]
Stream GetFile(string id);
Upvotes: 1
Reputation: 7876
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);
}
Upvotes: 9