上传.doc文件将出现错误:
将截断字符串或二进制数据
代码如下:
if (fileUpload.HasFile)
{
//取得文件的扩展名,并转换成小写
string fileExtension = System.IO.Path.GetExtension(fileUpload.FileName).ToLower();
//限定只能上传jpg和gif图片
// string[] allowExtension = { ".doc", ".txt" };
//对上传的文件的类型进行一个个匹对
string a = ".doc";
string b = ".txt";
if (fileExtension == a || fileExtension == b)
{
}
else
{
Label1.Text = "要上传的文件类型不对!";
}
//对上传文件的大小进行检测,限定文件最大不超过1M
if (fileUpload.PostedFile.ContentLength > 2048000)
{
Label1.Text = "上传文件大小超过2M";//上传文件超过限制
}
else
{
try
{
System.IO.Stream streamFile = this.fileUpload.PostedFile.InputStream;//建立数据流对象
int fileLength = this.fileUpload.PostedFile.ContentLength;//文件长度以字节为单位
byte[] fileData = new Byte[fileLength];//新建一个数组
streamFile.Read(fileData, 0, fileLength);//将这个数据流读取到数组中
SqlConnection conn;
SqlCommand cmd;
string cmdString = @"Insert TeacherMessage(姓名,性别,民族,政治面貌,出生年月,学历,专业,毕业院校,婚姻状况,身份证号,联系地址,联系电话,Email,所属部门,状态,个人简历)
Values ('" + TextBox1.Text + "', '" + TextBox2.Text + "','" + TextBox3.Text + "','" + TextBox4.Text + "','" + TextBox5.Text + "','" + TextBox6.Text + "','" + TextBox7.Text + "','" + TextBox8.Text + "','" + TextBox9.Text + "','" + TextBox10.Text + "','" + TextBox11.Text + "','" + TextBox12.Text + "','" + TextBox13.Text + "','" + TextBox14.Text + "','" + TextBox15.Text + "',@fileData)";
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["schoolhoomConnectionString1"].ConnectionString);
cmd = new SqlCommand(cmdString, conn);
cmd.Parameters.Add("@fileData", SqlDbType.Binary);
cmd.Parameters["@fileData"].Value = fileData;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Response.Redirect("tijiaochenggong.aspx");
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}
}
else
{
Response.Write("<script language=javascript>alert('不能上传空文件')</script>");
}
------解决方案--------------------------------------------------------
------解决方案--------------------------------------------------------
将截断字符串或二进制数据
数据库字段扩大。