我有这个代码,当我上传文件并查看gridview时,它看起来像下面的图片,我不知道为什么。此外,ContentType列也显示为空,我怀疑这与搞砸的上传有关。评论和GUID有效。没有出现错误消息。
protected void ButtonSubmit_Click(object sender, EventArgs e)
{
try
{
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
string filePath = FileUpload1.PostedFile.FileName;
string filename = Path.GetFileName(filePath);
string ext = Path.GetExtension(filename);
string contenttype = String.Empty;
switch (ext)
{
case ".doc":
contenttype = "application/vnd.ms-word";
break;
case ".docx":
contenttype = "application/vnd.ms-word";
break;
case ".xls":
contenttype = "application/vnd.ms-excel";
break;
case ".xlsx":
contenttype = "application/vnd.ms-excel";
break;
case ".jpg":
contenttype = "image/jpg";
break;
case ".png":
contenttype = "image/png";
break;
case ".gif":
contenttype = "image/gif";
break;
case ".pdf":
contenttype = "application/pdf";
break;
}
Stream fs = FileUpload1.PostedFile.InputStream;
BinaryReader br = new BinaryReader(fs);
Byte[] bytes = br.ReadBytes((Int32)fs.Length);
Guid newGUID = Guid.NewGuid();
conn.Open();
string InsertUser = "INSERT INTO UserUpload (ID, Comment,DataUpload, ContentType) VALUES (@ID, @Comment, @DataUpload, @ContentType)";
SqlCommand com = new SqlCommand(InsertUser, conn);
com.Parameters.AddWithValue("@ID", newGUID.ToString());
com.Parameters.AddWithValue("@Comment", TextBoxComment.Text);
com.Parameters.AddWithValue("@DataUpload", SqlDbType.VarChar).Value = bytes;
com.Parameters.Add("@ContentType", SqlDbType.VarChar).Value = contenttype;
com.ExecuteNonQuery();
LabelMessage.Text = ("Your Upload Is Complete");
conn.Close();
}
catch (Exception ex)
{
LabelMessage.Text = ("Error:" + ex.Message);
}
}