【发布时间】:2016-12-03 02:26:59
【问题描述】:
您好,我正在使用此 C# 代码上传图片。
protected void UploadFile(object sender, EventArgs e)
{
string filename = Path.GetFileName(FileUpload1.PostedFile.FileName);
string contentType = FileUpload1.PostedFile.ContentType;
using (Stream fs = FileUpload1.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (MySqlConnection con = new MySqlConnection(constr))
{
string query = "INSERT INTO foto(FileName, ContentType, Content) VALUES (@FileName, @ContentType, @Content)";
using (MySqlCommand cmd = new MySqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@FileName", filename);
cmd.Parameters.AddWithValue("@ContentType", contentType);
cmd.Parameters.AddWithValue("@Content", bytes);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
}
Response.Redirect(Request.Url.AbsoluteUri);
}
我在 longblob 字段中上传图像,然后显示图像我使用 C# WebService、AJAX、JavaScript,将图像转换为 Base64String,但图像显示为不存在。
这是我的 Base64String:
如您所见,问题在于这些额外的字符:
AAEAAAD/////AQAAAAAAAAAPAQAAAHgBAAAC
为什么会这样?我该如何解决?
【问题讨论】:
-
那是二进制格式的序列化头,很奇怪。你看过表中的数据吗?它是否包括那些前导字节?如果不是,您需要在 ToBase64String 调用之前显示加载和读取数据的代码