J S
J S

Reputation: 97

Upload image to database c#

I have a file upload tool in c# which allows users to upload an image but it only goes to my computer. Is there anyway I can make it upload to my mysql database?

Code for fileupload:

public void FileUpload(object sender, EventArgs e)
    {

        string fileName = FileUpload1.PostedFile.FileName;
        string extension = Path.GetExtension(fileName);
        if (extension.Equals(".gif") || extension.Equals(".jpg") || extension.Equals(".png"))           
        {
            string path = Server.MapPath("~/");              
            FileUpload1.SaveAs(path + fileName);
            Response.Write("File uploaded successfully");
    }
        else
        {
            Response.Write("File types: jpg, gif or png only.");
        }
    }

There's also a button that when clicked it uploads the file.

Any feedback would be appreciated. Thanks

Upvotes: 1

Views: 2505

Answers (1)

Minh
Minh

Reputation: 361

There are 2 ways to store image in database

1/ internal way you must create a BLOB column in database table.

CREATE TABLE tblname(ID INT,IMAGE BLOB);

INSERT INTO tblname(ID,IMAGE) VALUES(1,LOAD_FILE('C:/test.jpg'));

2/ external way: store your path of images in database

CREATE TABLE tblname(ID INT,IMAGE VARCHAR(20));    
INSERT INTO tblname(ID,IMAGE) VALUES(1,'C:/test.jpg');

Upvotes: 2

Related Questions