文件上传下载出现Byte[]和string的转换失败
错误:
上传错误:将参数值从 Byte[] 转换到 String 失败
下载错误:无法将类型为“System.String”的对象强制转换为类型“System.Byte[]”。
代码如下:
public partial class Default2 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
this.databindTodg();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
if (this.fileUpload.PostedFile.FileName == "")
{
Response.Write("<script language=javascript>alert('不能上传空文件')</script>");
return;
}
else
{
try
{
string path = Server.MapPath("upload/");//保存上传文件的文件夹upload虚拟路径对应的实际路径
string filePath = this.fileUpload.PostedFile.FileName;//客户端文件的完全限定名
string serverPath = path + filePath.Substring(filePath.LastIndexOf("//") + 1);//上传的文件保存在服务器端的路径
string fileName = filePath.Substring(filePath.LastIndexOf("//") + 1);//文件名
string fileType = this.fileUpload.PostedFile.ContentType.ToString();//文件类型
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 gerenjianli(fileName,filePath,fileType,fileDate)
Values (@fileName,@filePath,@fileType,@fileData)";
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["schoolhoomConnectionString"].ConnectionString);
cmd = new SqlCommand(cmdString, conn);
cmd.CommandType = CommandType.StoredProcedure;//指定类型
cmd.Parameters.Add("@fileName", SqlDbType.VarChar);
cmd.Parameters["@fileName"].Value = fileName;
cmd.Parameters.Add("@filePath", SqlDbType.VarChar);
cmd.Parameters["@filePath"].Value = serverPath;
cmd.Parameters.Add("@fileType", SqlDbType.VarChar);
cmd.Parameters["@fileType"].Value = fileType;
cmd.Parameters.Add("@fileData", SqlDbType.NText);
cmd.Parameters["@fileData"].Value = fileData;
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
Response.Write("<script language=javascript>alert('文件上传成功!')</script>");
}
}
private void databindTodg()
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["schoolhoomConnectionString"].ConnectionString);
con.Open();
string sql = "select * from gerenjianli";
SqlDataAdapter sda = new SqlDataAdapter(sql, con);
DataSet ds = new DataSet();
sda.Fill(ds);
this.GridView1.DataSource = ds.Tables[0].DefaultView;
this.DataBind();
}
protected void Button2_Click(object sender, EventArgs e)
{
}
protected void btnDownLoad_Click(object sender, EventArgs e)
{
}
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
int fileID = Convert.ToInt32(e.CommandArgument.ToString());
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["schoolhoomConnectionString"].ConnectionString);
con.Open();
string sql = "select * from gerenjianli where fileID='" + fileID + "'";
SqlCommand cmd = new SqlCommand(sql, con);
SqlDataReader sdr = cmd.ExecuteReader();
sdr.Read();
Response.Buffer = true;
Page.Response.Clear();//清除缓冲区所有内容
Page.Response.ContentType = "application/octet-stream";
Page.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(sdr["fileName"].ToString()));
byte[] file = (Byte[])sdr["fileData"];//读出数据
int a = file.Length;
Response.BinaryWrite(file);
Response.Flush();
Response.End();
sdr.Close();
con.Close();
}
}