tags:

views:

235

answers:

5

Hi I am a fresher to C#,i am able to upload a file to server with less size but when i am trying to upload a size more than 5000k it is giving an exception.

Here is my C# code

private void UploadFile(string filename)
{
  try
  {
    PeopleMatrixService peopleMetrixService = new PeopleMatrixService();

    String strFile = System.IO.Path.GetFileName(filename);
    // TestUploader.Uploader.FileUploader srv = new TestUploader.Uploader.FileUploader();
    FileInfo fInfo = new FileInfo(filename);
    long numBytes = fInfo.Length;
    double dLen = Convert.ToDouble(fInfo.Length / 10000000);
    if (dLen < 8)
    {
      FileStream fStream = new FileStream(filename, FileMode.Open, FileAccess.Read);
      BinaryReader br = new BinaryReader(fStream);
      byte[] data = br.ReadBytes((int)numBytes);
      br.Close();
      string sTmp = peopleMetrixService.UploadFile(data, strFile);
      fStream.Close();
      fStream.Dispose();
      MessageBox.Show("File Upload Status: " + sTmp, "File Upload");
    }
    else
    {
      MessageBox.Show("The file selected exceeds the size limit for uploads.", "File Size");
    }
  }
  catch (Exception ex)
  {
    MessageBox.Show(ex.Message.ToString(), "Upload Error");
  }
}

and my code in webservice

[WebMethod]
public string UploadFile(byte[] f, string fileName)
{
  try
  {
    MemoryStream ms = new MemoryStream(f);
    FileStream fs = new FileStream(System.Web.Hosting.HostingEnvironment.MapPath("~/Response Attachments/") + fileName, FileMode.Create);
              //FileStream fs = new FileStream(Server.MapPath("~/Response Attachments/") + fileName, FileMode.Create);
    ms.WriteTo(fs);
    ms.Close();
    fs.Close();
    fs.Dispose();
    return "OK";
  }
  catch (Exception ex)
  {
    return ex.Message.ToString();
  }
}
+2  A: 

What is the max size you have set in your web.config file? Check this.

Shoban
+3  A: 

Its the 'httpruntime' part in your web.config, eg:

<configuration>
  <system.web>
    <httpRuntime executionTimeout="300" maxRequestLength="20480" />
  </system.web>
</configuration>
Chris
+5  A: 

By default the maxRequestLength is set to 4096 (4mb) you need to change it to a larger size in your web.config file.

see: http://msdn.microsoft.com/en-us/library/e1f13641(VS.80).aspx

Element
A: 

By default, a web server will accept uploads smaller than 4MB in size; the web.config file must be updated in order to support larger uploads.

Mitch Wheat
A: 

Place this in your web.config

  <system.web>
     <httpRuntime executionTimeout="360" maxRequestLength="100000" />

That enables a 360 second timeout and 100,000 Kb of upload data at a time.

If that doesn't work, run this command on your IIS server. (replace [IISWebsitename])

C:\Windows\System32\inetsrv>appcmd set config "[IISWebsitename]" -section:requestFiltering -requestLimits.maxAllowedContentLength:100000000 -commitpath:apphost

That enables 100,000,000 bytes of upload data at a time.

Carter