0

I searched about how I could save and store large video size greater than 4MB into database using asp.net . I tried a many codes. also I used :

<httpRuntime maxRequestLength="102400" executionTimeout="3600" />


<security >
  <requestFiltering>
    <requestLimits maxAllowedContentLength="1024000000" />
  </requestFiltering>
</security>

It is not work. It show me message :

timeout expired. the timeout period elapsed prior to completion of the operation or the server is not responding

so far, I changed data column into varbinary(MAX) but I cann't store large file with size 100MB to up.

My code

 protected void Button1_Click(object sender, EventArgs e)
{
    // Read the file and convert it to Byte Array
    string filePath = FileUpload1.PostedFile.FileName;
    string filename = Path.GetFileName(filePath);
    string ext = Path.GetExtension(filename);
    string contenttype = String.Empty;


    if (ext == ".jpg" || ext == ".gif" || ext == ".png" || ext == ".bmp " || ext == " .jpeg")
    {
        Stream fs = FileUpload1.PostedFile.InputStream;
        BinaryReader br = new BinaryReader(fs);
        Byte[] bytes = br.ReadBytes((Int32)fs.Length);

        //insert the file into database

        string strQuery = "insert into Image(Img_Title, Img_Extension, Img_Data, Course_code, Course_num, Img_Description, Keyword , Date)" +
           " values (@Img_Title, @Img_Extension, @Img_Data, @Course_code, @Course_num, @Img_Description, @Keyword , @Date)";
        SqlCommand cmd = new SqlCommand(strQuery);
        cmd.Parameters.Add("@Img_Title", SqlDbType.VarChar).Value = filename;
        cmd.Parameters.Add("@Img_Extension", SqlDbType.VarChar).Value = ext;
        cmd.Parameters.Add("@Img_Data", SqlDbType.Binary).Value = bytes;
        cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = DateTime.Now;
        cmd.Parameters.Add("@Course_code", Course_code.SelectedItem.Text);
        cmd.Parameters.Add("@Course_num", Course_num.SelectedItem.Text);
        cmd.Parameters.Add("@Img_Description", Description.Text);
        cmd.Parameters.Add("@Keyword", keywords.Text);
        InsertUpdateData(cmd);
        Label2.ForeColor = System.Drawing.Color.Green;
        Label2.Text = "تم رفع الملف بنجاح";
    }


  else
    if (ext == ".avi" || ext == ".wmv" || ext == ".wma" || ext == ".wma " || ext == " .wma" || ext == ".rmvb" || ext == ".mp4" || ext == ".3gp " || ext == " .mkv" || ext == ".flv")
    {

        Byte[] buffer;
        Stream fs = FileUpload1.PostedFile.InputStream;
        BinaryReader br = new BinaryReader(fs);

            HttpPostedFile file = FileUpload1.PostedFile;//retrieve the HttpPostedFile object
            buffer = br.ReadBytes((int)FileUpload1.PostedFile.InputStream.Length);
            int bytesReaded = file.InputStream.Read(buffer, 0, FileUpload1.PostedFile.ContentLength);
            if (bytesReaded > 0)
            {


            // FileUpload1.SaveAs(Server.MapPath("../WebSite41/Videos/" + filenam));

                //insert the file into database

                string strQuery = "insert into [Video File](V_Title, V_Extension, Data, Course_code, Course_num, V_Description, Keyword,videoSize)" +
                                 " values (@V_Title, @V_Extension, @Data,  @Course_code, @Course_num, @V_Description, @Keyword ,@videoSize)";
                SqlCommand cmd = new SqlCommand(strQuery);
               // cmd.CommandTimeout = 90; 
                cmd.Parameters.Add("@V_Title", SqlDbType.VarChar).Value = FileUpload1.FileName;
                cmd.Parameters.Add("@V_Extension", SqlDbType.VarChar).Value = ext;
                //cmd.Parameters.AddWithValue("@Data", buffer);
                cmd.Parameters.AddWithValue("@Data", "../foldername/Videos/" + filename);
                cmd.Parameters.Add("@Course_code", Course_code.SelectedItem.Text);
                cmd.Parameters.Add("@Course_num", Course_num.SelectedItem.Text);
                cmd.Parameters.Add("@V_Description", Description.Text);
                cmd.Parameters.Add("@Keyword", keywords.Text);
                cmd.Parameters.Add("@videoSize", SqlDbType.BigInt).Value = file.ContentLength;
                InsertUpdateData(cmd);
                Label2.ForeColor = System.Drawing.Color.Green;
                Label2.Text = "تم رفع الملف بنجاح";
            }
        }


    else if (ext == ".doc" || ext == ".docx" || ext == ".docm" || ext == ".dotm" || ext == ".dotx" ||
         ext == ".xls " || ext == " .xlsx" || ext == " .xlsb" || ext == " .xlam" ||
         ext == ".ppt" || ext == ".pptx" || ext == ".pptm" || ext == ".potx" || ext == ".ppam" || ext == ".ppsx" || ext == ".ppsm" ||
         ext == ".sldx" || ext == ".sldm" || ext == ".thmx" ||
         ext == ".pdf" || ext == ".txt ")
    {
        Stream fs = FileUpload1.PostedFile.InputStream;
        BinaryReader br = new BinaryReader(fs);
        Byte[] bytes = br.ReadBytes((Int32)fs.Length);

        //insert the file into database

        string strQuery = "insert into [Text File](T_Title, T_Extension, T_Data, Course_code, Course_num, T_Description, T_Keyword,Date)" +
            " values (@T_Title, @T_Extension, @T_Data, @Course_code, @Course_num, @T_Description, @T_Keyword,@Date)";
        SqlCommand cmd = new SqlCommand(strQuery);
        cmd.Parameters.Add("@T_Title", SqlDbType.VarChar).Value = filename;
        cmd.Parameters.Add("@T_Extension", SqlDbType.VarChar).Value = ext;
        cmd.Parameters.Add("@T_Data", SqlDbType.Binary).Value = bytes;
        cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = DateTime.Now;
        cmd.Parameters.Add("@Course_code", Course_code.SelectedItem.Text);
        cmd.Parameters.Add("@Course_num", Course_num.SelectedItem.Text);
        cmd.Parameters.Add("@T_Description", Description.Text);
        cmd.Parameters.Add("@T_Keyword", keywords.Text);
        InsertUpdateData(cmd);
        Label2.ForeColor = System.Drawing.Color.Green;
        Label2.Text = "تم رفع الملف بنجاح";
    }


    else if (ext.ToLower() == ".html" || ext.ToLower() == ".htm")
    {
        Stream fs = FileUpload1.PostedFile.InputStream;
        BinaryReader br = new BinaryReader(fs);
        Byte[] bytes = br.ReadBytes((Int32)fs.Length);

        //insert the file into database

        string strQuery = "insert into [HTML File](H_Title, H_Extension, H_Data, Course_code, Course_num, H_Description, H_Keyword,Date)" +
           " values (@H_Title, @H_Extension, @H_Data, @Course_code, @Course_num, @H_Description, @H_Keyword ,@Date)";
        SqlCommand cmd = new SqlCommand(strQuery);
        cmd.Parameters.Add("@H_Title", SqlDbType.VarChar).Value = filename;
        cmd.Parameters.Add("@H_Extension", SqlDbType.VarChar).Value = ext;
        cmd.Parameters.Add("@H_Data", SqlDbType.Binary).Value = bytes;
        cmd.Parameters.Add("@Date", SqlDbType.DateTime).Value = DateTime.Now;
        cmd.Parameters.Add("@Course_code", Course_code.SelectedItem.Text);
        cmd.Parameters.Add("@Course_num", Course_num.SelectedItem.Text);
        cmd.Parameters.Add("@H_Description", Description.Text);
        cmd.Parameters.Add("@H_Keyword", keywords.Text);
        InsertUpdateData(cmd);
        Label2.ForeColor = System.Drawing.Color.Green;
        Label2.Text = "تم رفع الملف بنجاح";


        }




    else
    {
        Label2.ForeColor = System.Drawing.Color.Red;
        Label2.Text = "Error" +
          " Upload Image/Text formats";

    }
  }


private Boolean InsertUpdateData(SqlCommand cmd)
{
    String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
    SqlConnection con = new SqlConnection(strConnString);
    cmd.CommandType = CommandType.Text;
    cmd.Connection = con;
    try
    {
        con.Open();
        cmd.ExecuteNonQuery();
        return true;
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);
        return false;
    }
    finally
    {
        con.Close();
        con.Dispose();
    }

}

Thanks ...

Dalal
  • 27
  • 8
  • When you upload, or download a file is better to use a handler or if you have a page disable the session - see this similar answer: http://stackoverflow.com/questions/17441528/a-bug-in-win7-about-asp-net-download-file-to-client-browser/17442847#17442847 Also is better to save the video on the hard disk, but this is some other discusion. – Aristos Jul 08 '13 at 12:44
  • that mean,I must add user through session and I can determine timeout to upload and download file is large, isn't it? – Dalal Jul 08 '13 at 20:26

1 Answers1

0

Try changing the datatype of the field you are using to store the large file to image instead of binary

and in your code use :

cmd.Parameters.Add("@PARAMETER", SqlDbType.Image).Value = bytes;

instead of SqlDbType.Binary

also please note that you should increase your timeout in your connection string in case you need more time

value="Server=###.###.###.###;Database=$$$$$$$;User ID=##;Password=$$$$$$;Connection Timeout= 1200"

sm_
  • 2,572
  • 2
  • 17
  • 34
  • Where can I add this : value="Server=###.###.###.###;Database=$$$$$$$;User ID=##;Password=$$$$$$;Connection Timeout= 1200" ? – Dalal Jul 08 '13 at 19:23
  • in your connection string and as it appears in your code, it is in the configuration file (app.config / web.config if it is a web application) – sm_ Jul 09 '13 at 09:35
  • and you just need to add the connection timeout parameter, but have you tried what i suggested before you modify your connection timeout ? – sm_ Jul 09 '13 at 09:36
  • I changed datatype to image and used the code but it didn't work,too. show me : The connection was reset – Dalal Jul 10 '13 at 08:54
  • are you still getting timeout expired ? – sm_ Jul 10 '13 at 12:50
  • Did you try increasing your timeout from the connection string ? – sm_ Jul 11 '13 at 09:20
  • I used :... it sorted until 27MB but when I uploaded video, it show :(String or binary data would be truncated. The statement has been terminated.) – Dalal Jul 11 '13 at 10:39
  • this is not the connection string if you copy paste for me your web .config i will point you to the connection string. although increasing this one would help you in upload large files – sm_ Jul 11 '13 at 10:46
  • I tried to use (Connection Timeout) show error >>The connection was reset – Dalal Jul 14 '13 at 10:20
  • Am sorry i could not provide you with more help. Goodluck – sm_ Jul 15 '13 at 16:49
  • Thanks :) , I found solution of the problem here http://stackoverflow.com/questions/3853767/maximum-request-length-exceeded.. – Dalal Jul 16 '13 at 10:25
  • @Dalal Great :) but you completely mislead me, you told me the problem was executing the insert to SQL, while it was actually with uploading the file to server-side. Am glad it is solved anyway – sm_ Jul 16 '13 at 10:33