Click here to Skip to main content
15,886,509 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to upload files using streaming. when a user selected a file in browser it is converted to stream
is is done as follows.


client side code:

if (FileUpload1.HasFile)
        { 
            System.IO.FileInfo fileInfo = new System.IO.FileInfo(FileUpload1.PostedFile.FileName);
            FileTrasferServiceReference.TransferServiceClient clientUpload = new TransferServiceClient();
            using (System.IO.FileStream stream = new System.IO.FileStream(FileUpload1.PostedFile.FileName, System.IO.FileMode.Open, System.IO.FileAccess.Read))
            {                
                clientUpload.UploadFile(stream); 
            }
        } 


Wcf Service side code:
public void UploadFile(System.IO.Stream FileByteStream)
    {
        FileStream targetStream = null;
        Stream sourceStream =  FileByteStream;

        string uploadFolder = @"C:\upload\";
        //string filename = request.FileMetaData.Filename;
        string filePath = Path.Combine(uploadFolder, "xyz.zip");

        using (targetStream = new FileStream(filePath, FileMode.Create, FileAccess.Write, FileShare.None))
        {
            //read from the input stream in 4K chunks
            //and save to output stream
            const int bufferLen = 4096;
            byte[] buffer = new byte[bufferLen];
            int count = 0;
            while ((count = sourceStream.Read(buffer, 0, bufferLen)) > 0)
            {
                targetStream.Write(buffer, 0, count);
            }
            targetStream.Close();
            sourceStream.Close();
        }

    } 


the files are not being uploaded, is the above code correct or wrong? if not correct can some one provide sample code of it.
Posted
Comments
Anupama Roy 26-Jan-11 6:15am    
why are you using streaming?Is there any specific reason for that like very large files or anything else?

I recommend that you put try/catch blocks around everything, and then run it under the debugger.

It might be that your WCF connection isn't being established.
 
Share this answer
 
this is the same person who posted the comment in the comment a above i was not able add the code properly so i am adding it here.

I have established the wcf connection
server side binding
===================
binding name="TransferService"
                 transferMode="Streamed"
                 messageEncoding="Text"
                 maxReceivedMessageSize="2147483647">
             <readerQuotas maxDepth="128" maxStringContentLength="2147483647"
                 maxArrayLength="2147483647" maxBytesPerRead="4096" maxNameTableCharCount="2147483647" />
             <security mode="None">
               <transport clientCredentialType="None" proxyCredentialType="None"
                realm="" />
               <message clientCredentialType="UserName" algorithmSuite="Default" />
             </security>
           </binding



behavior name="TransferServiceBehavior">
                 <serviceMetadata httpGetEnabled="true" />
               <dataContractSerializer maxItemsInObjectGraph="2147483647" />
                 <serviceDebug includeExceptionDetailInFaults="true" />
             </behavior




Client side binding
==================
 
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