如何从MySQL上传和检索图像
问题描述:
亲爱的先生,
现在我正在做一个Web应用程序(Asp.net/c#).在这个项目中,我想将图像上传到mysql,也必须在gridview中显示图像.一个字段如longblob,我也可以将图像上传到mysql.显示数据库大小.但是我无法将图像检索到gridview.我在gridview中看不到任何东西.这就是上传代码
Dear Sir,
Now i am doing one web application(Asp.net/c#).In this project i want to upload images to mysql and also i have to show images in gridview.Here i am taking images by using fileupload control.In database i have set one field as longblob and also i can upload images to mysql.In database size is showing.But i can not retrieve images to gridview.I can not see anything in gridview.This is uploading code
int imageFileSize = FileUpload1.PostedFile.ContentLength;
BinaryReader imageFileBinaryReader = new BinaryReader(FileUpload1.FileContent);
byte[] imageFileBinaryBuffer = imageFileBinaryReader.ReadBytes(imageFileSize);
MySqlParameter imageFileBinaryParam = new MySqlParameter("@Log", MySqlDbType.VarBinary, imageFileSize);
imageFileBinaryParam.Value = imageFileBinaryBuffer;
cmdsave.Parameters.Add(imageFileBinaryParam);
cmdsave.executenonquery();
这是处理程序类中的检索代码
This is the retrieving code in handler class
HttpRequest imageRequest = context.Request;
HttpResponse imageResponse = context.Response;
MySqlDataReader imageFromDbReader = imageFromDbCmd.ExecuteReader(CommandBehavior.SingleRow);
if (imageFromDbReader.HasRows)
{
imageFromDbReader.Read();
string imageFileMIMEType = imageFromDbReader["ImageFileMIMEType"].ToString();
string imageFileSize = imageFromDbReader["ImageFileSize"].ToString();
byte[] ImageFileBinaryData = (byte[])imageFromDbReader["Logo"];
imageResponse.ContentType = imageFileMIMEType;
imageResponse.AddHeader("ImageFileSize", imageFileSize);
BinaryWriter imageFromDbWriter = new BinaryWriter(imageResponse.OutputStream);
imageFromDbWriter.Write(ImageFileBinaryData, 0, ImageFileBinaryData.Length);
imageFromDbWriter.Close();
所以请帮帮我.
谢谢
Sujith
So please help me.
Thank You
Sujith