| | |
storing file from a FileUpload control to a database
Please support our C# advertiser: Intel Parallel Studio Home
![]() |
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
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.
The sample code that uses the hardcoded location is as follows: it also requires that the data already exists in the table
Has anyone any ideas? I am rather new to programming.... help help help lol
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
C# Syntax (Toggle Plain Text)
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);
path = "File:" + FileUpload_uploadBrief.FileName;
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();
command.Parameters["@AssignmentBrief"].Value = bblob;//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
C# Syntax (Toggle Plain Text)
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
Please move your thread to ASP.NET forum http://www.daniweb.com/forums/forum18.html
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
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
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 BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
C# Syntax (Toggle Plain Text)
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)
Hope your problem got solved, if it did, please mark your thread as solved, else tell me what's up??
BI Developer | LINKdotNET
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
B.Sc Computer Science, Helwan University
Technical blog | http://ramymahrous.wordpress.com
LinkedIn | http://www.linkedin.com/in/ramymahrous
![]() |
Other Threads in the C# Forum
- Previous Thread: Implementing a logviewer using listview
- Next Thread: Validating Form Input for HTML Tags but anchor tag
| Thread Tools | Search this Thread |
.net access ado.net algorithm array barchart bitmap box broadcast buttons c# chat check checkbox client color combobox control conversion csharp custom database datagrid datagridview dataset datetime degrees development draganddrop drawing enabled encryption enum event excel file files form format forms function gdi+ httpwebrequest image index input install java label list listbox listener load mandelbrot math mouseclick mysql networking operator path photoshop picturebox pixelinversion post prime programming radians regex remote remoting resolved. richtextbox save saving serialization server sleep socket sql statistics stream string table tcp text textbox thread time timer treeview update usercontrol validation view visualstudio webbrowser windows winforms wpf xml






