
You can use the below code:-
Client side:
WCFClient.ServiceReference1.FileTransferServiceClient client=
new WCFClient.ServiceReference1.FileTransferServiceClient();
string uploadfilename = "Upload.txt";
using (FileStream fs = new FileStream(@"C:\"+uploadfilename, FileMode.Open, FileAccess.Read, FileShare.Read))
{
client.UploadFile(uploadfilename, fs); <font color="red">// There is I have an error!!!</font>
}
Server side:
[ServiceContract]
public interface IFileTransferService
{
[OperationContract(IsOneWay = true)]
void UploadFile(FileUploadMessage request);
[OperationContract(IsOneWay = false)]
FileDownloadReturnMessage DownloadFile(FileDownloadMessage request);
}
[MessageContract]
public class FileUploadMessage
{
[MessageHeader(MustUnderstand = true)]
public string Filename;
[MessageBodyMember(Order = 1)]
public Stream FileByteStream;
}
[MessageContract]
public class FileDownloadMessage
{
[MessageHeader(MustUnderstand = true)]
public string Filename;
}
[MessageContract]
public class FileDownloadReturnMessage
{
public FileDownloadReturnMessage(string filename, Stream stream)
{
this.DownloadedFilename = filename;
this.FileByteStream = stream;
}
[MessageHeader(MustUnderstand = true)]
public string DownloadedFilename;
[MessageBodyMember(Order = 1)]
public Stream FileByteStream;
}
public class ServiceClass : IFileTransferServiceMarco
{
public void UploadFile(FileUploadMessage request)
{
//try
{
string basePath = @"C:\Downloads";
string serverFileName = Path.Combine(basePath, request.Filename);
using (FileStream outfile = new FileStream(serverFileName, FileMode.Create))
{
const int bufferSize = 65536; // 64K
Byte[] buffer = new Byte[bufferSize];
int bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize);
while (bytesRead > 0)
{
outfile.Write(buffer, 0, bytesRead);
bytesRead = request.FileByteStream.Read(buffer, 0, bufferSize);
}
}
}
// catch (IOException e)
{
// throw new FaultException<ioexception>(e);
}
}
public FileDownloadReturnMessage DownloadFile(FileDownloadMessage request)
{
string localFileName = request.Filename;
try
{
string basePath = @"C:\Downloads";
string serverFileName = Path.Combine(basePath, request.Filename);
Stream fs = new FileStream(serverFileName, FileMode.Open);
return new FileDownloadReturnMessage(request.Filename, fs);
}
catch (IOException e)
{
throw new FaultException<ioexception>(e);
}
}
}</ioexception></ioexception>
and app.config: Client side
<bindings>
<basicHttpBinding>
<binding name="BasicHttpBinding_IFileTransferServiceMarco" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:10:00"
allowCookies="false" bypassProxyOnLocal="false" hostNameComparisonMode="StrongWildcard"
maxBufferSize="65536" maxBufferPoolSize="524288" maxReceivedMessageSize="4294967294"
messageEncoding="Text" textEncoding="utf-8" transferMode="Streamed"
useDefaultWebProxy="true">
<readerQuotas maxDepth="32" maxStringContentLength="8192" maxArrayLength="16384"
maxBytesPerRead="4096" maxNameTableCharCount="16384" />
<security mode="None">
<transport clientCredentialType="None" proxyCredentialType="None"
realm="" />
<message clientCredentialType="UserName" algorithmSuite="Default" />
</security>
</binding>
</basicHttpBinding>
</bindings>
<endpoint address="http://localhost:8080/EssentialWCF/winceStream"
binding="basicHttpBinding" bindingConfiguration="BasicHttpBinding_IFileTransferServiceMarco"
contract="ServiceReference1.IFileTransferServiceMarco" name="BasicHttpBinding_IFileTransferServiceMarco" />
Server side Binding:
<bindings>
<basicHttpBinding>
<binding name="HttpBinding_MTOM" messageEncoding="Text" transferMode="Streamed" maxReceivedMessageSize="4294967294"
maxBufferSize="65536" maxBufferPoolSize="65536"
</binding>
</basicHttpBinding>
</bindings>
<endpoint address="winceStream"
binding="basicHttpBinding"
bindingConfiguration="HttpBinding_MTOM"
contract="EssentialWCF.IFileTransferServiceMarco" />
You can see the link :
http://www.codeproject.com/Questions/185332/Send-large-file-to-WCF-service-and-vise-versa
http://www.codeproject.com/Articles/166763/WCF-Streaming-Upload-Download-Files-Over-HTTP
Happy Coding.
If it helps you or directs U towards the solution,
MARK IT AS ANSWER Sudhakar_A, if this helps please login to Mark As Answer. | Alert Moderator