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

G

Guest

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,HighestQualificationID,JobFieldID,TotalExperienceInYears,CandidatePresentLocation,FileName,CandidateResume,type,length)
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",txtCandidateName.Text)
Cmd.Parameters.Add("@FileName",System.IO.Path.GetFileName(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
 
J

Joerg Jooss

Himanshu said:
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,
 
G

Guest

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();
 
J

Joerg Jooss

Himanshu said:
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/issues/04/04/CuttingEdge/default.aspx

Cheers,
 
G

Guest

thanks joerg,

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

Regards

Himanshu
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top