I'm trying to implement file upload into my WCF Service in asp .net C#
Here is the code in WCF Server for File uploading.
public void FileUpload(string fileName, Stream fileStream)
{
FileStream fileToupload = new FileStream("D:\\FileUpload\\" + fileName, FileMode.Create);
// byte[] bytearray = new byte[10000];
byte[] bytearray = new byte[1000];
int bytesRead, totalBytesRead = 0;
do
{
bytesRead = fileStream.Read(bytearray, 0, bytearray.Length);
totalBytesRead += bytesRead;
if(bytesRead > 0)
fileToupload.Write(bytearray, 0, bytearray.Length);
} while (bytesRead > 0);
// fileToupload.Write(bytearray, 0, bytearray.Length);
fileToupload.Close();
fileToupload.Dispose();
}
Here is the code for Client to upload a File: (Fixed Byte Array Size)
protected void bUpload_Click(object sender, EventArgs e)
{
byte[] bytearray = null;
Stream stream;
string fileName = "";
//throw new NotImplementedException();
if (FileUpload1.HasFile)
{
fileName = FileUpload1.FileName;
stream = FileUpload1.FileContent;
stream.Seek(0, SeekOrigin.Begin);
bytearray = new byte[stream.Length];
int count = 0;
while (count < stream.Length)
{
bytearray[count++] = Convert.ToByte(stream.ReadByte());
}
}
string baseAddress = "http://localhost/WCFService/Service1.svc/FileUpload/";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(baseAddress + fileName);
request.Method = "POST";
request.ContentType = "text/plain";
Stream serverStream = request.GetRequestStream();
serverStream.Write(bytearray, 0, bytearray.Length);
serverStream.Close();
try
{
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
int statusCode = (int)response.StatusCode;
System.Diagnostics.Debug.WriteLine("statusCode: " + statusCode);
StreamReader reader = new StreamReader(response.GetResponseStream());
System.Diagnostics.Debug.WriteLine("reader: " + reader.ToString());
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("--- EXCEPTION ---");
ex.ToString();
}
}
This is working good with a small size file, When I tried with bigger size files I changed fixed size byte array to a dynamic byte array Writing to stream. Here is updated code: (chunks of 1024 bytes of Byte array used to Send data)
request.Method = "POST";
request.ContentType = "text/plain";
// Stream serverStream = request.GetRequestStream();
if (FileUpload1.HasFile)
{
fileName = FileUpload1.FileName;
stream = FileUpload1.FileContent;
stream.Seek(0, SeekOrigin.Begin);
bytearray = new byte[1024];//stream.Length];
}
int TbyteCount = 0;
Stream requestStream = request.GetRequestStream();
int bufferSize = 1024;
byte[] buffer = new byte[bufferSize];
int byteCount = 0;
while ((byteCount = stream.Read(buffer, 0, bufferSize)) > 0)
{
TbyteCount = TbyteCount + byteCount;
requestStream.Write(buffer, 0, byteCount);
}
requestStream.Close();
try
{
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
int statusCode = (int)response.StatusCode;
System.Diagnostics.Debug.WriteLine("statusCode: " + statusCode);
StreamReader reader = new StreamReader(response.GetResponseStream());
System.Diagnostics.Debug.WriteLine("reader: " + reader.ToString());
}
}
catch (Exception ex)
{
System.Diagnostics.Debug.WriteLine("--- EXCEPTION ---");
ex.ToString();
}
}
But While reading response I get Exception The remote server returned an error: (413) Request Entity Too Large.
Am I doing correct with writing multiple times in request stream !?
I used a file size 22.4 KB, it is successfully uploaded using 1st code (fixed size array) If I split file size in multiples of 1024 bytes and tried to send then there is a problem.
Web.config file
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="aspnet:UseTaskFriendlySynchronizationContext" value="true"/>
</appSettings>
<system.web>
<compilation debug="true"/>
</system.web>
<system.serviceModel>
<services>
<service name="WcfServiceApp.Service1" behaviorConfiguration="ServiceBehavior">
<endpoint address="" binding="webHttpBinding" contract="WcfServiceApp.IService1" behaviorConfiguration="webBehaviour"/>
<host>
<baseAddresses>
<add baseAddress="http://localhost:50327/Service1.svc"/>
</baseAddresses>
</host>
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="ServiceBehavior">
<!-- To avoid disclosing metadata information, set the values below to false before deployment -->
<serviceMetadata httpGetEnabled="true" httpsGetEnabled="true"/>
<!-- To receive exception details in faults for debugging purposes, set the value below to true. Set to false before deployment to avoid disclosing exception information -->
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
<endpointBehaviors>
<behavior name="webBehaviour">
<webHttp/>
</behavior>
</endpointBehaviors>
</behaviors>
<!--<protocolMapping>
<add binding="basicHttpsBinding" scheme="https"/>
</protocolMapping>-->
<serviceHostingEnvironment aspNetCompatibilityEnabled="true" multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
<system.webServer>
<httpProtocol>
<customHeaders>
<add name="Access-Control-Allow-Origin" value="*"/>
<add name="Access-Control-Allow-Headers" value="Content-Type, Accept"/>
</customHeaders>
</httpProtocol>
<directoryBrowse enabled="true"/>
</system.webServer>
</configuration>