Upload and Download to folder using WCF

Sep 14 2014 9:43 AM
I am working on upload and download files using wcf.I am able download large files .But I am able to upload only small files. Here is my code
 
Server Config File 
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Client.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web>
<customErrors mode="Off" />
<compilation debug="true" targetFramework="4.5"/>
<httpRuntime maxRequestLength="1006710888"/>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</system.web>
<system.serviceModel>
<bindings>
<basiHttpBinding>
<binding name="ws" maxReceivedMessageSize="2000000" messageEncoding="Mtom" />
<binding name="BasicHttpBinding_IService1" maxReceivedMessageSize="2000000" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
hostNameComparisonMode="StrongWildcard" >
<readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxStringContentLength="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647"/>
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8066/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>
<appSettings>
<add key="aspnet:MaxHttpCollectionKeys" value="20000" />
</appSettings>
<userSettings>
<Client.Properties.Settings>
<setting name="downloadFile" serializeAs="String">
<value />
</setting>
<setting name="uploadFile" serializeAs="String">
<value />
</setting>
</Client.Properties.Settings>
</userSettings>
</configuration>
 
Client web.config
<?xml version="1.0"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="Client.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<system.web>
<customErrors mode="Off" />
<compilation debug="true" targetFramework="4.5"/>
<httpRuntime maxRequestLength="1006710888"/>
<pages controlRenderingCompatibilityVersion="3.5" clientIDMode="AutoID"/>
</system.web>
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="ws" maxReceivedMessageSize="2000000" messageEncoding="Mtom" />
<binding name="BasicHttpBinding_IService1" maxReceivedMessageSize="2000000" closeTimeout="00:01:00"
openTimeout="00:01:00" receiveTimeout="00:10:00" sendTimeout="00:01:00"
hostNameComparisonMode="StrongWildcard" >
<readerQuotas maxArrayLength="2147483647" maxBytesPerRead="2147483647" maxStringContentLength="2147483647" maxDepth="2147483647" maxNameTableCharCount="2147483647"/>
<security mode="None" />
</binding>
</basicHttpBinding>
</bindings>
<client>
<endpoint address="http://localhost:8066/Service1.svc" binding="basicHttpBinding"
bindingConfiguration="BasicHttpBinding_IService1" contract="ServiceReference1.IService1"
name="BasicHttpBinding_IService1" />
</client>
</system.serviceModel>
<appSettings>
<add key="aspnet:MaxHttpCollectionKeys" value="20000" />
</appSettings>
<userSettings>
<Client.Properties.Settings>
<setting name="downloadFile" serializeAs="String">
<value />
</setting>
<setting name="uploadFile" serializeAs="String">
<value />
</setting>
</Client.Properties.Settings>
</userSettings>
</configuration>
 
Server .cs File
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Microsoft.SharePoint.Client;
using Microsoft.SharePoint.Client.Search;
using Microsoft.SharePoint.Client.Search.Query;
using System.IO;
using System.Collections;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using System.Data;
using System.ComponentModel;
using Microsoft.Office.Server.Search.Query;
using System.Net;
using Microsoft.SharePoint.Search.Query;
using Microsoft.SharePoint.Client.Taxonomy;
using System.Web.Configuration;
namespace NewWCFServiceChange
{
[ServiceBehavior(InstanceContextMode = InstanceContextMode.PerSession)]
public class Service1 : IService1
{
public string StrClientFileName = "";
public void UploadAFile(string filename, string strFilePath, string strAutoNUmber, string strmetadata, string strMetadataTags,string filePathcombine)
{
string Concat = "ORDID";
string strTAGS = "";
int Number = 0;
string Numbersplit = "";
using (ClientContext ctx = new ClientContext("http://machinename/sites/First"))
{
Web web = ctx.Web;
List uploadList = ctx.Web.Lists.GetByTitle("UploadList");
ctx.Load(uploadList);
ctx.ExecuteQuery();
strTAGS = hiddencreateterms(strMetadataTags);
int listCount = uploadList.ItemCount;
ListItemCreationInformation listCreationInformation = new ListItemCreationInformation();
ListItem oListItem = uploadList.AddItem(listCreationInformation);
oListItem["Title"] = filename;
oListItem["Docid"] = Number;
oListItem["DocPath"] = strFilePath;
oListItem["DownID"] = listCount+1;
oListItem["TaxKeyword"] = strTAGS;
oListItem["Metadataadd"] = strmetadata;
oListItem["OrderID"] = strAutoNUmber;
oListItem.Update();
ctx.ExecuteQuery();
}
}
 
Server Interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using Microsoft.SharePoint.Client;
using System.IO;
using System.Collections;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;
using System.Data;
using System.ComponentModel;
using Microsoft.Office.Server.Search.Query;
using System.ServiceModel;
namespace NewWCFServiceChange
{
[ServiceContract]
public interface IService1
{
[OperationContract]
void UploadAFile(string filename, string strFilePath, string strAutoNUmber, string MetataData, string strMetadataTags,string filePathcombine);
[OperationContract]
void UploadFileAsPackets(RemoteFileInfo request);
[OperationContract]
RemoteFileInfoForDownload DownloadFileAsPackets(DownloadRequest request);
}
[MessageContract]
public class DownloadRequest
{
[MessageBodyMember]
public string FileName;
}
[MessageContract]
public class RemoteFileInfoForDownload
{
[MessageHeader(MustUnderstand = true)]
public string FileName;
[MessageHeader(MustUnderstand = true)]
public long Length;
[MessageBodyMember(Order = 1)]
public System.IO.Stream FileByteStream;
}
[MessageContract]
public class RemoteFileInfo : IDisposable
{
[MessageHeader(MustUnderstand = true)]
public string FileName;
[MessageHeader(MustUnderstand = true)]
public long Length;
[MessageHeader(MustUnderstand = true)]
public string strOrderID;
[MessageHeader(MustUnderstand = true)]
public string strmetadata;
[MessageHeader(MustUnderstand = true)]
public string strMetadataTags;
[MessageHeader(MustUnderstand = true)]
public string strDocPath;
[MessageBodyMember(Order = 1)]
public System.IO.Stream FileByteStream;
public void Dispose()
{
if (FileByteStream != null)
{
FileByteStream.Close();
FileByteStream = null;
}
}
}
}
Client .CS File
public void UploadFileAsPackets(RemoteFileInfo request)
{
// create output folder, if does not exist
if (!System.IO.Directory.Exists(@"c:\uploadedfiles")) System.IO.Directory.CreateDirectory(@"c:\uploadedfiles");
// kill target file, if already exists
//string filePath = System.IO.Path.Combine("Upload", request.FileName);
string filePath = @"\\MachineName\uploadedfiles\" + request.FileName;// ; System.IO.Path.Combine("Upload", request.FileName);
if (System.IO.File.Exists(filePath)) System.IO.File.Delete(filePath);
int chunkSize = 2048;
byte[] buffer = new byte[chunkSize];
using (System.IO.FileStream writeStream = new System.IO.FileStream(filePath, System.IO.FileMode.CreateNew, System.IO.FileAccess.Write))
{
do
{
// read bytes from input stream
int bytesRead = request.FileByteStream.Read(buffer, 0, chunkSize);
if (bytesRead == 0) break;
// write bytes to output stream
writeStream.Write(buffer, 0, bytesRead);
} while (true);
writeStream.Close();
}
string filePathcombine = filePath;
string strmetadata= request.strmetadata;
string strMetadataTags=request.strMetadataTags;
string strOrderID=request.strOrderID;
UploadAFile(request.FileName, filePathcombine, strOrderID, strmetadata, strMetadataTags,filePathcombine);
}
public RemoteFileInfoForDownload DownloadFileAsPackets(DownloadRequest request)
{
// get some info about the input file
string filePath = @"\\Machinename\uploadedfiles\"+request.FileName;
System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
// check if exists
if (!fileInfo.Exists) throw new System.IO.FileNotFoundException("File not found", request.FileName);
// open stream
System.IO.FileStream stream = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
// return result
RemoteFileInfoForDownload result = new RemoteFileInfoForDownload();
result.FileName = request.FileName;
result.Length = fileInfo.Length;
result.FileByteStream = stream;
return result;
}
}
}
 
 
Client Side Reference File
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.ServiceModel;
using System.Text;
using System.IO;
using System.Collections;
using System.Data;
using System.ComponentModel;
namespace NewWCFClient
{
public class StreamWithProgress : Stream
{
private readonly FileStream file;
private readonly long length;
public class ProgressChangedEventArgs : EventArgs
{
public long BytesRead;
public long Length;
public ProgressChangedEventArgs(long BytesRead, long Length)
{
this.BytesRead = BytesRead;
this.Length = Length;
}
}
public event EventHandler<ProgressChangedEventArgs> ProgressChanged;
private long bytesRead;
public StreamWithProgress(System.IO.FileStream file)
{
this.file = file;
length = file.Length;
bytesRead = 0;
if (ProgressChanged != null) ProgressChanged(this, new ProgressChangedEventArgs(bytesRead, length));
}
public double GetProgress()
{
return ((double)bytesRead) / file.Length;
}
public override bool CanRead
{
get { return true; }
}
public override bool CanSeek
{
get { return false; }
}
public override bool CanWrite
{
get { return false; }
}
public override void Flush() { }
public override long Length
{
get { throw new Exception("The method or operation is not implemented."); }
}
public override long Position
{
get { return bytesRead; }
set { throw new Exception("The method or operation is not implemented."); }
}
public override int Read(byte[] buffer, int offset, int count)
{
int result = file.Read(buffer, offset, count);
bytesRead += result;
if (ProgressChanged != null) ProgressChanged(this, new ProgressChangedEventArgs(bytesRead, length));
return result;
}
public override long Seek(long offset, SeekOrigin origin)
{
throw new Exception("The method or operation is not implemented.");
}
public override void SetLength(long value)
{
throw new Exception("The method or operation is not implemented.");
}
public override void Write(byte[] buffer, int offset, int count)
{
throw new Exception("The method or operation is not implemented.");
}
}
}
 
I need help on this.
 
Thanks
KK