By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,375 Members | 1,342 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,375 IT Pros & Developers. It's quick & easy.

problem in uploading and downloading files from DB in ASP.Net

P: n/a
hi,

Can anybody tell me that thru asp.net using c#, how can we upload and
download physical files in any table of SQL Server Database.

the uploading part is running successfully but the problem arises in the
retriving part of the code.
i am not getting that how will i able to download the file which is there in
the SQL Server database in the field type "image".

here is the code by which i am uploading any file to the my SQL Server
database

private void btnUpload_Click(object sender, System.EventArgs e)
{
HttpPostedFile filPosted = filUpload.PostedFile;
int intFileLength = System.Convert.ToInt32(filPosted.ContentLength);
byte[] byteData = new byte[intFileLength];
filPosted.InputStream.Read(byteData, 0, intFileLength);
Conn.OpenConnection();
string sql="Insert into
HRRecruitmentMaster(CandidateName,HighestQualifica tionID,JobFieldID,TotalExperienceInYears,Candidate PresentLocation,FileName,CandidateResume,type,leng th)
Values (@CandidateName," + cmbHighestQuali.SelectedValue + "," +
cmbJobField.SelectedValue + "," + txtTotalExp.Text + ",'" +
txtPresentLoc.Text + "',@FileName,@CandidateResume,@Type," + intFileLength +
")";
SqlCommand Cmd = new SqlCommand();
Cmd.CommandText=sql;
Cmd.Connection =Conn.Cn;
Cmd.Parameters.Add("@CandidateName",txtCandidateNa me.Text)
Cmd.Parameters.Add("@FileName",System.IO.Path.GetF ileName(filPosted.FileName))
Cmd.Parameters.Add("@CandidateResume",System.Data. SqlDbType.Image,intFileLength);
Cmd.Parameters["@CandidateResume"].Value=byteData;
Cmd.Parameters.Add("@Type",filPosted.ContentType);
Cmd.ExecuteNonQuery();
Conn.CloseConnection();
}
so pls. help me out in this matter i'll be very much grateful to u

Regards

Himanshu Saxena
Sahara India
India
Nov 19 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
Himanshu wrote:
hi,

Can anybody tell me that thru asp.net using c#, how can we upload and
download physical files in any table of SQL Server Database.

the uploading part is running successfully but the problem arises in
the retriving part of the code.
i am not getting that how will i able to download the file which is
there in the SQL Server database in the field type "image".


I don't really get your problem. Is that a design issue or a
programming issue?

You could simply display all downloadable files using a DataList or a
Repeater, and link all files to a DownloadPage.aspx that pulls a linked
file from the database and writes it to the response stream.

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 19 '05 #2

P: n/a
hi joreg

thanks for replying me
ya.. my problem is only this that i dont know that how to download the
physical file stored in a table of SQL server database (datatype "image").
the piece of code which i written to download the file is given below:

now the problem is in type casting the image file into the (httppostedfile)
(the red line code).
pls. tell me that how to covert the physical file which is coming from
database to the streaming file?

or if u have any suggestion, pls. let me know.
Code:
SqlDataAdapter da = new SqlDataAdapter("select
FileName,CandidateResume,type,length from HRRecruitmentMaster where
HRCVID=1",Conn.Cn);

DataSet ds = new DataSet();

da.Fill(ds);

ShowMessageBox(ds.Tables[0].Rows[0]["FileName"].ToString());
int nFileLen=Convert.ToInt32(ds.Tables[0].Rows[0]["length"]);

string sFilename =ds.Tables[0].Rows[0]["FileName"].ToString();

HttpPostedFile a =(HttpPostedFile) ds.Tables[0].Rows[0]["CandidateResume"];
byte[] myData = new Byte[nFileLen];

a.InputStream.Read(myData,0,nFileLen);

System.IO.FileStream newFile = new
System.IO.FileStream(Server.MapPath("C:\\" +
sFilename),System.IO.FileMode.Create);

newFile.Write(myData,0,myData.Length);

newFile.Close();
"Joerg Jooss" wrote:
Himanshu wrote:
hi,

Can anybody tell me that thru asp.net using c#, how can we upload and
download physical files in any table of SQL Server Database.

the uploading part is running successfully but the problem arises in
the retriving part of the code.
i am not getting that how will i able to download the file which is
there in the SQL Server database in the field type "image".


I don't really get your problem. Is that a design issue or a
programming issue?

You could simply display all downloadable files using a DataList or a
Repeater, and link all files to a DownloadPage.aspx that pulls a linked
file from the database and writes it to the response stream.

Cheers,
--
http://www.joergjooss.de
mailto:ne********@joergjooss.de

Nov 19 '05 #3

P: n/a
Himanshu wrote:
hi joreg

thanks for replying me
ya.. my problem is only this that i dont know that how to download
the physical file stored in a table of SQL server database (datatype
"image"). the piece of code which i written to download the file is
given below:

now the problem is in type casting the image file into the
(httppostedfile) (the red line code).
pls. tell me that how to covert the physical file which is coming
from database to the streaming file?


You cannot use HttpPostedFile for this. You simply have to write the
file's bytes stored in the database to the response stream, using
HttpResponse.BinaryWrite().

You can also do it the other way around. Create an Image object from
the file's bytes and use Image.Save() to write the Image to the
response stream. See
http://msdn.microsoft.com/msdnmag/is...e/default.aspx

Cheers,

--
http://www.joergjooss.de
mailto:ne********@joergjooss.de
Nov 19 '05 #4

P: n/a
thanks joerg,

the problem is solved now. and the code is running successfully

Regards

Himanshu

"Joerg Jooss" wrote:
Himanshu wrote:
hi joreg

thanks for replying me
ya.. my problem is only this that i dont know that how to download
the physical file stored in a table of SQL server database (datatype
"image"). the piece of code which i written to download the file is
given below:

now the problem is in type casting the image file into the
(httppostedfile) (the red line code).
pls. tell me that how to covert the physical file which is coming
from database to the streaming file?


You cannot use HttpPostedFile for this. You simply have to write the
file's bytes stored in the database to the response stream, using
HttpResponse.BinaryWrite().

You can also do it the other way around. Create an Image object from
the file's bytes and use Image.Save() to write the Image to the
response stream. See
http://msdn.microsoft.com/msdnmag/is...e/default.aspx

Cheers,

--
http://www.joergjooss.de
mailto:ne********@joergjooss.de

Nov 19 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.