我搜索了如何使用 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();
}
}
谢谢 ...