简体   繁体   中英

Load picture from SQL database asp.net C#

I have avatars stored in a database. I have used the the data type image for the avatars. I want to load the avatar of a user into an image control, but i cant get it to work. I have tried this code without any luck:

public void GetUserAvatar()
    {
        string username = Convert.ToString(Session["username"]);

        var image = from u in dc.Users
                    where u.username == username
                    select u.image;
        imgAvatar.Controls.Add(image);
    }

I think i might have to save the images to a folder and then save the file path in the database instead. This seems to be easier and smoother. Any ideas?

I ended up saving the image to a folder and then saving the path to the database. My code for image uploading now looks like this:

public void addImage()
    {
        if (fuAvatar.PostedFile.ContentType.ToLower().StartsWith
            ("image") && fuAvatar.HasFile)
        {
            string saveLocation = Server.MapPath("savedAvatars/");
            string fileExtension = System.IO.Path.GetExtension(fuAvatar.FileName);
            string fileName = Convert.ToString(Session["userid"]);
            string savePath = saveLocation + fileName + fileExtension;
            fuAvatar.SaveAs(savePath);

            string imageDBPath = fileName + fileExtension;

            LinqClass1DataContext dc = new LinqClass1DataContext();
            int userid = Convert.ToInt32(Session["userid"]);
            var tblUser = (from u in dc.Users
                           where u.userid == userid
                           select u).First();
            tblUser.imagePath = @"\savedAvatars\"+imageDBPath;
            dc.SubmitChanges();
            lblResult.Text = "Avatar lastet opp!";
        }
        else lblResult.Text = "Avatar ikke lastet opp!";
    }

And to load the picture:

public void GetUserAvatar()
   {
       int userid = Convert.ToInt32(Session["userid"]);

       var varPath = dc.Users.Single(u => (u.userid == userid)).imagePath;

       string imagePath = Convert.ToString(varPath);
       imgAvatar.ImageUrl = imagePath;
   }

you should add a generic handler file (.ashx) and write in it something like that:

string sql = "select image from table1";
SqlCommand command = new SqlCommand(sql, con);
SqlDataReader dReader = command.ExecuteReader();
dReader.Read();
context.Response.BinaryWrite((byte[])dReader["Source"]);
dReader.Close();

then in your page do something like this:

img.ImageUrl = "Handler.ashx;

http://www.dotnetcurry.com/ShowArticle.aspx?ID=129 read this article, first I've found. Basically you need to create a handler which responds with the raw image.

Please find the answer in below link.

Get images from database using Handler

You will have to create a handler which will load the image and u can pass the source to image tag.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM