如何从MySQL上传和检索图像 [英] How to Upload and Retrieve Image from Mysql

查看:80
本文介绍了如何从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

推荐答案

使用ASP.NET在MySQL中存储图像 [ ^ ]


这篇关于如何从MySQL上传和检索图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆