473,385 Members | 1,898 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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

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
4 2132
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

14
by: James Wong | last post by:
Hi! everybody, I'm facing a quite strange download problem. I use the following code to download an XML file to client side: With Response ' clear buffer Call .Clear() ' specify the...
2
by: D. Shane Fowlkes | last post by:
I'm about to build in some functionality where my client's staff can do some kind of basic upload to a folder or series of folders on our web server and allow authenticated users to know the UID...
2
by: Matt Mercer | last post by:
Hi all, I have a database application (asp .net vb) where I need to upload and download files to a SQL database. I am not going to store the files in the database...just the file name. All the...
0
by: TJ | last post by:
Hi, I've written code web-based uploading and downloading. Here is some code for it. For saving file into MS-SQL database, SaveFileIntoDB(HttpPostedFile file) { int fileLength =...
0
by: just.starting | last post by:
I am having problem while downloading files from an apache server2.0.53 with php4.3.10.While downloading some files it generally stops after downloading some specific amount and then stops...
2
by: Volki | last post by:
I am using file upload download aspx code on my IIS 6.0, FW 1.1. I am uploading the files properly. However I am having problems on downloading the files in this code. Most of the browers I do not...
3
by: OriginalBrownster | last post by:
I am currently uploading a file from a users computer to the file system on my server using python, just reading the file and writing the binaries. total_data=' ' while True: data =...
15
by: =?ISO-8859-1?Q?J=F8rn?= Dahl-Stamnes | last post by:
Hello folks, I need some help/advice FAST. I have problems with addslashes on my web-servers. After uploading a file, I read the uploaded file, use addslashes on the read data and then insert...
3
by: muziburrehaman | last post by:
i am looking for code in php to upload the 1 gb files. any one can please help me by sending the code....
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.