Reputation: 53
I've looked everywhere and tried a lot of fixes to solve ths problem but i'm at a loss and maybe its just something i cant see. I have a ASP.NET website with a WCF service which is use to upload a file, i can upload small files (althought 16kb turns out to be 977 at the other end) but when i upload a larger file (150kb) i get an error The remote server returned an error: (413) Request Entity Too Large. I increased the max size of content which can be uploaded but as far as i have read thats all i needed to do. So i guess u have 2 questions:
My Web service is fine but for the sake of it here is the code:
[OperationContract]
[WebInvoke(Method = "POST", UriTemplate = "FileUpload/{fileName}")]
void FileUpload(string fileName, Stream fileStream);
public void FileUpload(string fileName, Stream fileStream)
{
FileStream fileToupload = new FileStream("C:\\" + fileName, FileMode.Create);
byte[] bytearray = new byte[1000000];
int bytesRead, totalBytesRead = 0;
do
{
bytesRead = fileStream.Read(bytearray, 0, bytearray.Length);
totalBytesRead += bytesRead;
} while (bytesRead > 0);
fileToupload.Write(bytearray, 0, bytearray.Length);
fileToupload.Close();
fileToupload.Dispose();
}
My web.config file is as follows
<system.serviceModel>
<bindings>
<basicHttpBinding>
<binding name="uploadfilebinding" closeTimeout="10:01:00"
maxBufferSize="204857600" maxBufferPoolSize="204857600"
maxReceivedMessageSize="104857600" openTimeout="10:01:00"
receiveTimeout="10:10:00" sendTimeout="10:01:00"
messageEncoding="Mtom" transferMode="StreamedRequest">
<readerQuotas maxDepth="204857600" maxStringContentLength="204857600"
maxArrayLength="204857600" maxBytesPerRead="204857600"
maxNameTableCharCount="204857600" />
</binding>
</basicHttpBinding>
</bindings>
<services>
<service name="sUploadFile.UploadFile"
behaviorConfiguration="uploadfilebehavior">
<endpoint
address=""
binding="basicHttpBinding"
bindingConfiguration="uploadfilebinding"
contract="sUploadFile.UploadFile">
<identity>
<dns value="localhost" />
</identity>
</endpoint>
<endpoint
address="mex"
binding="mexHttpBinding" contract="IMetadataExchange" />
</service>
</services>
<behaviors>
<serviceBehaviors>
<behavior name="">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
<behavior name="uploadfilebehavior">
<serviceMetadata httpGetEnabled="true"/>
<serviceDebug includeExceptionDetailInFaults="false"/>
</behavior>
</serviceBehaviors>
</behaviors>
<serviceHostingEnvironment multipleSiteBindingsEnabled="true"/>
</system.serviceModel>
And i'm callin it as follows:
string baseServiceAddress = "http://" + Environment.MachineName + ":8000/suploadfile.svc/FileUpload";
ServiceHost host = new ServiceHost(typeof(sUploadFile), new Uri(baseServiceAddress));
host.AddServiceEndpoint(typeof(IsUploadFile), new WebHttpBinding(), "").Behaviors.Add(new WebHttpBehavior());
host.Open();
byte[] bytearray=null ;
string name = "";
//throw new NotImplementedException();
if (FileUpload1.HasFile)
{
name = FileUpload1.FileName;
Stream 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://" + Environment.MachineName + ":8000/suploadfile.svc/FileUpload/";
HttpWebRequest request = (HttpWebRequest) HttpWebRequest.Create(baseAddress+name);
request.Method = "POST";
request.ContentType = "text/plain";
try
{
Stream serverStream = request.GetRequestStream();
serverStream.Write(bytearray, 0, bytearray.Length);
serverStream.Close();
using (HttpWebResponse response = request.GetResponse() as HttpWebResponse)
{
int statusCode = (int)response.StatusCode;
StreamReader reader = new StreamReader(response.GetResponseStream());
}
}
catch (Exception ess)
{
}
}
Upvotes: 4
Views: 3865
Reputation: 46
Change
byte[] bytearray = new byte[1000000];
to:
byte[] bytearray = new byte[fileSize+1];
Upvotes: 3