1

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

1
  • 1
    You can insert it has a blob into the database using a prepared statement and named parameters. Just load the file as a byte[] array and use it as a parameter for the INSERT statement. Commented Dec 8, 2013 at 0:47

1 Answer 1

2

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');
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.