asp.net を使用して、4MB を超える大きなビデオ サイズをデータベースに保存して保存する方法を検索しました。たくさんのコードを試しました。また、私は使用しました:
<httpRuntime maxRequestLength="102400" executionTimeout="3600" />
<security >
<requestFiltering>
<requestLimits maxAllowedContentLength="1024000000" />
</requestFiltering>
</security>
それは仕事ではありません。それは私にメッセージを表示します:
タイムアウトになりました。操作が完了する前にタイムアウト時間が経過したか、サーバーが応答していません
これまで、データ列を varbinary(MAX) に変更しましたが、サイズが 100MB までの大きなファイルを保存できません。
私のコード
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();
}
}
ありがとう ...