Hi,
I need some guidence or some tutorials which will teach me how to do ,
i am not sure is this the correct forum i am in.

i need a way to upload any kind of file from the fileUpload control to MySQL server how can i do this,
i searched in the internet and i got one with php but i am not using it,
appreciate if some one could help me
thanks

Dani AI

Generated

A few practical, long‑lasting points addressing ’s compile errors and ’s filesystem recommendation.

If you can, prefer saving the uploaded file on disk and keeping only metadata (path, original name, MIME type, size, created date) in MySQL. That keeps the database smaller, makes backups and restores faster, and lets the web server serve static files efficiently. Store files outside the webroot or in a protected folder (App_Data or a separate storage path), give files a GUID-based filename to avoid collisions, and keep the original filename in the DB for display.

The compile errors come from using a two‑dimensional array where a one‑dimensional byte[] is required. Use a chunked read to avoid allocating huge buffers and to work with any BLOB size. Example pattern for reading a BLOB to disk:

int picCol = dr.GetOrdinal("Picture");
int bufferSize = 8192;
byte[] buffer = new byte[bufferSize];
long bytesRead;
long offset = 0;

using (var fs = new FileStream(destPath, FileMode.Create, FileAccess.Write))
{
    while ((bytesRead = dr.GetBytes(picCol, offset, buffer, 0, buffer.Length)) > 0)
    {
        fs.Write(buffer, 0, (int)bytesRead);
        offset += bytesRead;
    }
}

Quick extra tips: validate extension and MIME type, enforce size limits and update web.config/IIS settings for large uploads, use parameterized MySqlCommand with the appropriate BLOB type when inserting bytes, and avoid loading very large files fully into memory.

Recommended Answers

All 3 Replies

hey, i was trying the below link tutorial

but i get an error saying "cannot convert from 'byte[*,*]' to 'byte[]'" in the 8th line of the below code, why is this happening??

and for the 12th line "The best overloaded method match for 'System.IO.Stream.Write(byte[], int, int)' has some invalid arguments"
??

can some one give me a solution to this????

private void SqlBlob2File(string DestFilePath)
        {
            int PictureCol = 0; //  the column # of the BLOB field
            MySqlConnection cn = new MySqlConnection("server=localhost;integrated security=yes;database=NorthWind UId=root;Pwd=8780");
            MySqlCommand cmd = new MySqlCommand("SELECT Picture FROM Categories WHERE CategoryName=\'Test\'", cn);
            cn.Open();
            MySqlDataReader dr = cmd.ExecuteReader();
            dr.Read();
            byte[,] b;
            dr.GetBytes(PictureCol,0,b,0,b.Length);
            dr.Close();
            cn.Close();
            System.IO.FileStream fs = new System.IO.FileStream(DestFilePath, System.IO.FileMode.Create, System.IO.FileAccess.Write);
            fs.Write(b, 0, b.Length);
            fs.Close();
        }

hi dear
it is not recommended that you save your posted file in database

you must save the file name in a string field like: Pic_url

you must do :

protected void Button1_Click(object sender, EventArgs e)
    {
        //check fileupload has a file or not
        if (FileUpload1.HasFile)
        {
            //save filename as your pic_url field in your DB
            string FileName = FileUpload1.PostedFile.FileName;

            //now use path for physical Address
            string path = Server.MapPath(".");

            //now save your file
            FileUpload1.SaveAs(path + "\\" + FileName);
        }
    }

also you must combine your filename with a random name or datatime for anti duplicate string dateAndtime = DateTime.Now.ToString("yyyyMMddhhmmsstt"); FileName = dateAndtime + FileName;


if you want to check file extention and size please post your question,

hi dear
it is not recommended that you save your posted file in database

you must save the file name in a string field like: Pic_url

you must do :

protected void Button1_Click(object sender, EventArgs e)
    {
        //check fileupload has a file or not
        if (FileUpload1.HasFile)
        {
            //save filename as your pic_url field in your DB
            string FileName = FileUpload1.PostedFile.FileName;

            //now use path for physical Address
            string path = Server.MapPath(".");

            //now save your file
            FileUpload1.SaveAs(path + "\\" + FileName);
        }
    }

also you must combine your filename with a random name or datatime for anti duplicate
[ATTACH]17984[/ATTACH]

[ATTACH]17985[/ATTACH]

string dateAndtime = DateTime.Now.ToString("yyyyMMddhhmmsstt");
            FileName = dateAndtime + FileName;

if you want to check file extention and size please post your question,

ok so what you are saying is not to post the file into the database? why is it not a good solution?

you had said to only save the file name in the database combined with the datetime for example, where do i save the softcopy in the server??? can you give me more details on this..

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.