在asp.net网页中插入图片 [英] Image insertion in an asp.net webpage

查看:83
本文介绍了在asp.net网页中插入图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好...
在我的网页中,如何将图像存储在数据库中以及如何从数据库中检索图像.

Hi Guys...
In my webpage how can i store the images in database as well as retrieve from the database.

推荐答案

尝试以下链接:
http://www.aspdotnet-suresh.com/2011/01/how-to-insert-images-into-database-and.html [ http://www .ashishblog.com/blog/how-to-store-and-retrieve-image-in-database-using-c-asp-net-jquery/ [
try the foolowing links:
http://www.aspdotnet-suresh.com/2011/01/how-to-insert-images-into-database-and.html[^]
http://www.ashishblog.com/blog/how-to-store-and-retrieve-image-in-database-using-c-asp-net-jquery/[^]


检索很容易:
Retrieve is easy: A generic Image-From-DB class for ASP.NET[^]

Insert is harder:
1) Add a Upload control to your page.
2) Handle the Click event, and call this:
/// <summary>
/// Save an upload into the database.
/// </summary>
/// <param name="fl">Control containing the download.</param>
/// <returns>Status as a string</returns>
private string SaveUpload(FileUpload fl)
    {
    if (fl.HasFile)
        {
        try
            {
            int version = 0;
            string filename = Path.GetFileName(fl.FileName);
            byte[] filedata = fl.FileBytes;
            string strCon = ConnectionStrings.Download;
            using (SqlConnection con = new SqlConnection(strCon))
                {
                con.Open();
                // Check version - if the file exists in the DB already, then increase version number.
                using (SqlCommand ver = new SqlCommand("SELECT MAX(version) FROM dlContent WHERE fileName=@FN", con))
                    {
                    ver.Parameters.AddWithValue("@FN", filename);
                    object o = ver.ExecuteScalar();
                    if (o != null && o != System.DBNull.Value)
                        {
                        // Exists already.
                        version = (int) o + 1;
                        }
                    }
                // Stick file into database.
                using (SqlCommand ins = new SqlCommand("INSERT INTO dlContent (iD, fileName, description, dataContent, version, uploadedOn) " +
                                                       "VALUES (@ID, @FN, @DS, @DT, @VS, @UD)", con))
                    {
                    ins.Parameters.AddWithValue("@ID", Guid.NewGuid());
                    ins.Parameters.AddWithValue("@FN", filename);
                    ins.Parameters.AddWithValue("@DS", "");
                    ins.Parameters.AddWithValue("@DT", filedata);
                    ins.Parameters.AddWithValue("@VS", version);
                    ins.Parameters.AddWithValue("@UD", DateTime.Now);
                    ins.ExecuteNonQuery();
                    }
                }
            return string.Format("{0} uploaded, version = {1}", filename, version);
            }
        catch (Exception ex)
            {
            return "The file could not be uploaded. The following error occured: " + ex.Message;
            }
        }
    return "Please select a file.";
    }


这篇关于在asp.net网页中插入图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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