【发布时间】:2012-03-15 05:30:07
【问题描述】:
如果有办法Upload file using rest via stream 是否也有“Download”?如果是,你能告诉我怎么做吗?提前致谢!
【问题讨论】:
如果有办法Upload file using rest via stream 是否也有“Download”?如果是,你能告诉我怎么做吗?提前致谢!
【问题讨论】:
我用来从我的 REST 服务下载文件的示例方法:
[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);
}
【讨论】:
你也可以使用下面的
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);
}
当它被定义为时
[WebGet(UriTemplate = "file/{id}")]
[OperationContract]
Stream GetFile(string id);
【讨论】: