1.11M Members

storing file from a FileUpload control to a database

 
0
 

Hi, I would really appreciate some help.

I have a FileUpload control on my Asp.Net web page and want to store the file to a database as a varbinary(max) BLOB with other data via an sproc. However I am getting an error at the line

FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);

ERROR = The given paths format is not supported

I found a sample piece of code that stores a file to a database but it needs a hardcoded File location string (which doesnt quite work with what I want from the fileUpload control). Although I implemented it as a test page and it stores the file (and opens it in another piece of code) just the way I want mine to work.

I know the file from the upload control is already in byte format and that I am creating a new byte array to store the file data but I dont know how to go about doing this other than the way I am currently trying to do it.

protected void Button1_Click(object sender, EventArgs e)
        {
            DateTime now = DateTime.Today.Date;
            DateTime due_date = DateTime.Parse(txt_dueDate_uploadBrief.Text);
            DateTime return_date = DateTime.Parse(txt_ReturnDate_UploadBrief.Text);
            string modID = txt_HiddenModuleID_UplaodBrief.Text.ToString();
            int module_ID = Convert.ToInt32(modID);
            [B]path = "File:" + FileUpload_uploadBrief.FileName;[/B]
            command = new SqlCommand("upload_upBrief", myConnection);
            command.CommandType = CommandType.StoredProcedure;
            myConnection.Open();

            FileStream fs = new FileStream(path, FileMode.Open, FileAccess.Read);             bblob = new byte[fs.Length];
            fs.Read(bblob, 0, bblob.Length);
            fs.Close();

            //Initialising new instances of SqlParameter that declares the data type, size and column name
            //for the data that is being passed in
            command.Parameters.Add(new SqlParameter("@assRefNo", SqlDbType.NVarChar, 50, "AssRefNo"));
            command.Parameters.Add(new SqlParameter("@AssignmentBrief", SqlDbType.VarBinary, -1, ParameterDirection.Input, false, 0, 0, "AssignmentBrief", DataRowVersion.Current, path));
            command.Parameters.Add(new SqlParameter("@releaseDate", SqlDbType.Date, 8, "ReleaseDate"));
            command.Parameters.Add(new SqlParameter("@dueDate", SqlDbType.Date, 10, "DueDate"));
            command.Parameters.Add(new SqlParameter("@returnDate", SqlDbType.Date, 8, "ReturnDate"));
            command.Parameters.Add(new SqlParameter("@moduleID", SqlDbType.Int, 4, "ModuleID"));

            ////Adds data based on the parameters passed in
            command.Parameters["@assRefNo"].Value = txt_AssRefNo_uploadBrief.Text.ToString();
            [B]command.Parameters["@AssignmentBrief"].Value = bblob;[/B]//FileUpload_uploadBrief.ToString();//test;//value;
            command.Parameters["@releaseDate"].Value = now;
            command.Parameters["@dueDate"].Value = due_date;
            command.Parameters["@returnDate"].Value = return_date;
            command.Parameters["@moduleID"].Value = module_ID;
}

The sample code that uses the hardcoded location is as follows: it also requires that the data already exists in the table

protected void Button1_Click(object sender, EventArgs e)
        {
            myConnection.Open();
          
            command = new SqlCommand("UPDATE Pub_info SET logo = @Picture WHERE CategoryName = 'me'", myConnection);
            fs = new FileStream("c:\\Builder.doc", FileMode.Open, FileAccess.Read);
            Byte[] blob = new Byte[fs.Length];
            fs.Read(blob, 0, blob.Length);
            fs.Close();
            param = new SqlParameter("@Picture", SqlDbType.VarBinary, blob.Length,
            ParameterDirection.Input, false, 0, 0, null, DataRowVersion.Current, blob);
            command.Parameters.Add(param);
            command.ExecuteNonQuery();
            
        }

Has anyone any ideas? I am rather new to programming.... help help help lol

 
0
 

I was going to put it there but my problem really isnt ASP its c# .... the FileUpload control can be used in windows forms and asp.net pages so ... plus Im new to this forum and dont know how to move the thread!

 
0
 

Never mind friend, I just ask you to get more help, copy it and open another thread in asp.net forum and drop it there and delete this one.

My general answer is to convert the file you get to binary or array of byte (Byte[]) then insert it in the column (of Binary, Image or Blob datatype) and don't forget to insert the extension if the file to help you convert it back when you need to use it again :) best of luck

 
0
 
Byte[] b = FileUpload_uploadBrief.FileBytes;
                        blob = b;

I got it working, the FileUpload was reading bytes already and I had no need to use FileStream class....all hunky dory now (well just that one wee bit)

 
0
 

Hope your problem got solved, if it did, please mark your thread as solved, else tell me what's up??

You
This article has been dead for over six months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article