Click here to Skip to main content
15,890,557 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
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:
<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>

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:
C#
[OperationContract, WebInvoke(UriTemplate = "/UploadFile/?filename={fileName}")]
        void UploadFile(string fileName, Stream fileContents);

Implementation:
C#
public void UploadFile(string fileName, Stream fileContents)
        {           
            using (FileStream fs = new FileStream(@"C:\Downloads\" + fileName, FileMode.Create, FileAccess.Write ))
            {             
                CopyStream(fileContents, fs);
            }
}

app.config above.

Could anyone explain me what's wrong?
Posted

1 solution

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! :(
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900