1

when the file size is 1.66 MB or bigger than that, an error occurred saying"file size exceed the length".How to enable the system to accept big file size as much as it can. the file saves in database and the column type is varbinary(max). Do not know what does cause this error!

The code is:

if (FileUpload1.HasFile)
{
    if (ext.Equals(".pdf"))
    {
        Stream fs = FileUpload1.PostedFile.InputStream;
        BinaryReader br = new BinaryReader(fs);
        Byte[] bytes = br.ReadBytes((Int32)fs.Length);

        string classNmae = ddClass.Text.Split('~')[0] + ddClass.Text.Split('1');
        com.Parameters.Clear();
        com.CommandText = "UPDATE [Marking] SET [fileName]=@fileName, [fileType]=@fileType, [type]=@typ,[submissionDate]=@sd, [filePath]=@fp where [class_id]=@cid AND [module_id]=@mid  AND [student_id]= '" + Session["id"].ToString() + "'";
        com.Parameters.Add("@cid", SqlDbType.VarChar).Value = ddClass.Text.Split('~')[0];
        com.Parameters.Add("@mid", SqlDbType.VarChar).Value = ddClass.Text.Split('~')[1];
        com.Parameters.Add("@fileName", SqlDbType.VarChar).Value = filename;
        com.Parameters.Add("@fileType", SqlDbType.VarChar).Value = "application/pdf";
        com.Parameters.Add("@typ", SqlDbType.VarChar).Value = txtType.Text;
        com.Parameters.Add("@sd", SqlDbType.VarChar).Value = DateTime.Now;
        com.Parameters.Add("@fp", SqlDbType.Binary).Value = bytes;

        com.ExecuteNonQuery();
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
Saif AL-Qiari
  • 469
  • 5
  • 20
  • 3
    Why aren't you using parameterized SQL for the student ID? Use parameters for *everything*. (That will be irrelevant to the problem, but you should absolutely do it anyway.) – Jon Skeet Dec 27 '14 at 19:54
  • Check the configuration of the IIS site related to maximum request size - http://ajaxuploader.com/large-file-upload-iis-asp-net.htm, **also, what Jon Skeet said!** – Lasse V. Karlsen Dec 27 '14 at 19:55
  • 1
    Also remember that a database is not a fileserver – huysentruitw Dec 27 '14 at 19:56
  • @WouterHuysentruit Although not really related to the code that the OP posted, some argument could be made that it's a fine line - http://technet.microsoft.com/en-us/library/bb933993%28v=sql.105%29.aspx – Lasse V. Karlsen Dec 27 '14 at 19:57
  • what do you by parameterized student ID? does it cause any problem in current code? – Saif AL-Qiari Dec 27 '14 at 20:24
  • @Algiri it's just strange that you're using parameters for everything except for `student_id` – huysentruitw Dec 27 '14 at 20:34
  • actually student_id is used as session and will not be updated when student upload file, however other columns are parameterized because they will be updated when a file uploaded. – Saif AL-Qiari Dec 27 '14 at 20:42
  • @Algiri that doesn't matter, just don't concatenate values into your query string. Use parameters. – huysentruitw Dec 27 '14 at 20:54

1 Answers1

4

In your web.config file:

<configuration>
    <system.web>
        <httpRuntime maxRequestLength="16384" />
    </system.web>
</configuration>

The value is in KB. In this case equals to 16MB.

dario
  • 5,149
  • 12
  • 28
  • 32