protected Int32 FileLength = 0; //记录文件长度变量
protected void Button_Submit(object sender, EventArgs e)
{
HttpPostedFile UpFile = UP_FILE.PostedFile; //HttpPostedFile对象,用于读取图象文件属性
FileLength = UpFile.ContentLength; //记录文件长度
try
{
if (FileLength == 0)
{ //文件长度为零时
txtMessage.Text = "<b>请你选择你要上传的文件</b>";
}
else
{
Byte[] FileByteArray = new Byte[FileLength]; //图象文件临时储存Byte数组
Stream StreamObject = UpFile.InputStream; //建立数据流对像
//读取图象文件数据,FileByteArray为数据储存体,0为数据指针位置、FileLnegth为数据长度
StreamObject.Read(FileByteArray, 0, FileLength);
//建立SQL Server链接
SqlConnection Con = new SqlConnection();
Con.ConnectionString = "Data Source=5C4B31A8922E479\\WZ;" + "Database=KGEManage;" + "Integrated Security=True";
String SqlCmd = "INSERT INTO ImageStore (ImageData, ImageContentType,ImageDescription, ImageSize) values (@Image, @ContentType,@ImageDescription, @ImageSize)";
SqlCommand CmdObj = new SqlCommand(SqlCmd, Con);
CmdObj.Parameters.Add("@Image", SqlDbType.Binary, FileLength).Value = FileByteArray;
CmdObj.Parameters.Add("@ContentType", SqlDbType.VarChar, 50).Value = UpFile.ContentType; //记录文件类型
//把其它单表数据记录上传
CmdObj.Parameters.Add("@ImageDescription", SqlDbType.VarChar, 200).Value = txtDescription.Text;
//记录文件长度,读取时使用
CmdObj.Parameters.Add("@ImageSize", SqlDbType.BigInt, 8).Value = UpFile.ContentLength;
Con.Open();
CmdObj.ExecuteNonQuery();
Con.Close();
txtMessage.Text = "<p><b>你已经成功上传你的图片</b>";//提示上传成功
}
}
数据库结构,ImageData 为Image类型,数据存储字段,理论可以存储2GB的二进制数据,但以上代码可以成功实现上传2M左右的文件,但是上传5M的就不行了,就出现页面无法打开,请高手指点,是什么原因造成的?该如何修改?
存大点的文件最好不要用数据库,用ftp的方式比较好