RESTful Service通过流上传/下载大文件 [英] RESTful Service Upload/Download large file through stream
问题描述
大家好!
我正在尝试创建RESTful服务,该服务通过流上传和下载大文件.我有VS2008/Framework 3.5
下载:
代码段(服务器端):
合同:
Hi all!
I''m trying to create RESTful service which upload and download big files through stream. I have VS2008/Framework 3.5
Download:
Code snippet(Server side):
Contract:
[OperationContract]
[WebGet(RequestFormat=WebMessageFormat.Xml, ResponseFormat=WebMessageFormat.Xml,
UriTemplate = "/DownloadFile/?filename={filename}")]
Stream DownloadFile(string filename);
实施:
Implementation:
public Stream DownloadFile(string filename)
{
string filenamefull = @"C:\downloads\" + filename;
using (FileStream fs = new FileStream(filenamefull, FileMode.Open, FileAccess.Read))
{
WebOperationContext ctx = WebOperationContext.Current;
ctx.OutgoingResponse.ContentType = "application/octet-stream";
ctx.OutgoingResponse.ContentLength = fs.Length;
return fs;
}
}
app.config:
app.config:
<webHttpBinding>
<binding name="streamWebHttpBinding" receiveTimeout="01:00:00" maxReceivedMessageSize="2147483648"
sendTimeout="01:00:00" transferMode="Streamed" />
</webHttpBinding
><endpoint address="rest" binding="webHttpBinding" contract="EssentialWCF.IRest"
behaviorConfiguration="behaviorRest" bindingConfiguration="streamWebHttpBinding"/>
<endpointBehaviors>
<behavior name="behaviorRest">
<webHttp />
</behavior>
</endpointBehaviors>
当我执行它时,我得到空的流!如果我将流更改为byte [],则效果很好!
-------------------------------------------------- -------------------------------
上传效果很好,但是当我运行WCF测试客户端时,出现如下错误:
When I execute it I get empty stream! If I change stream to byte[] it works great!
---------------------------------------------------------------------------------
Upload works great, but when I run WCF Test Client I get error like below:
Cannot obtain Metadata from http://localhost:8080/EssentialWCF/mex<br />
<br />
For request in operation UploadFile to be a stream the operation must have a single parameter whose type is Stream.
代码段:
合同:
Code snippet:
Contract:
[OperationContract, WebInvoke(UriTemplate = "/UploadFile/?filename={fileName}")]
void UploadFile(string fileName, Stream fileContents);
实施:
Implementation:
public void UploadFile(string fileName, Stream fileContents)
{
using (FileStream fs = new FileStream(@"C:\Downloads\" + fileName, FileMode.Create, FileAccess.Write ))
{
CopyStream(fileContents, fs);
}
}
上面的app.config.
谁能解释我怎么了?
app.config above.
Could anyone explain me what''s wrong?
推荐答案
我解决了下载文件的问题.我不应该使用运算符"using".因为使用"关闭流,而我的函数返回关闭流!
I solved my problem with downloading files. I shouldn''t used operator "using". Because "using" close stream and my function returns closed stream!
public Stream DownloadFile(string filename)
{
string filenamefull = @"C:\downloads\" + filename;
FileStream fs = new FileStream(filenamefull, FileMode.Open, FileAccess.Read);
return fs;
}
不幸的是,上传文件仍然出错! :(
Unfortunately, uploading file still getting error! :(
这篇关于RESTful Service通过流上传/下载大文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!