IT Community - Software Programming, Web Development and Technical Support

upload file

This is a discussion on upload file within the ASP and ASP.NET Programming forums, part of the Web Development category; Hello, I want to know how to upload image and doc files/ pdf files to the database through ASP.NET. ...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > ASP and ASP.NET Programming

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 02-19-2008, 05:57 AM
Kirubhananth Kirubhananth is offline
D-Web Programmer
 
Join Date: Feb 2008
Location: My native is Madurai but now in Chennai
Posts: 61
Kirubhananth is on a distinguished road
Send a message via AIM to Kirubhananth Send a message via Skype™ to Kirubhananth
Question upload file

Hello,
I want to know how to upload image and doc files/ pdf files to the database through ASP.NET.
Please give me example code.

-Kirubha
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 02-24-2008, 10:35 PM
Manikandan.S Manikandan.S is offline
D-Web Trainee
 
Join Date: Feb 2008
Posts: 8
Manikandan.S is on a distinguished road
Smile Re: upload file

hi
u can use file upload control

for eg:

string dir = "e:\\image\\";
sring fileName = FileUpload1.FileName;

FileUpload1.SaveAs(dir+ fileName);


// file is saved in specified directory
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-26-2008, 06:07 AM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: upload file

Hi Kirubha,

You can upload the images into database using the following code.

protected void BtnUpload_Click(object sender, EventArgs e)
{
Stream imgstream = FileUpload1.PostedFile.InputStream;
int imglen = FileUpload1.PostedFile.ContentLength;
string imgcontenttype = FileUpload1.PostedFile.ContentType;
string imgname = FileUpload1.FileName;
byte[] imgBinarydata = new byte[imglen];
int n = imgstream.Read(imgBinarydata, 0, imglen);
int rowsAffected = SaveToDb(imgname, imgBinarydata, imgcontenttype);
if (rowsAffected > 0)
{
Response.Write("The Image was saved in the Database");
}
else
{
Response.Write("The error occured uploading the image");
}
}
private int SaveToDb(string imgName, byte[] imgbin, string imgcontenttype)
{
string connectionString = System.Configuration.ConfigurationManager.AppSetti ngs["connectionstring"];
SqlConnection conn = new SqlConnection(connectionString);
SqlCommand cmd = new SqlCommand("insert into images(img_name, img_data, img_contenttype) values(@imgname, @imgdata, @imagecontenttype)", conn);
cmd.Parameters.Add("@imgname", SqlDbType.VarChar).Value = imgName;
cmd.Parameters.Add("@imgdata", SqlDbType.Image).Value = imgbin;
cmd.Parameters.Add("@imagecontenttype", SqlDbType.VarChar).Value = imgcontenttype;
conn.Open();
int numRowsAffected = cmd.ExecuteNonQuery();
conn.Close();
return numRowsAffected;
}
__________________
S.Balasubramanian
Nothing is impossible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-26-2008, 10:50 PM
Kirubhananth Kirubhananth is offline
D-Web Programmer
 
Join Date: Feb 2008
Location: My native is Madurai but now in Chennai
Posts: 61
Kirubhananth is on a distinguished road
Send a message via AIM to Kirubhananth Send a message via Skype™ to Kirubhananth
Question Re: upload file

Hi balu,

thank you. How to download the file from the DB and save it as a file in the disk?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-27-2008, 01:57 AM
rrrajesh84in rrrajesh84in is offline
D-Web Master
 
Join Date: Mar 2007
Posts: 399
rrrajesh84in is on a distinguished road
Default Re: upload file

hi

Its very simple just give the url in your address bar it ll get downloaded..
using this idea do the functionality in your code.
__________________
.....................................
''''''
Rajesh''''''
Ants. . . . . . Like me
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 03-13-2008, 03:19 AM
Balasubramanian.S Balasubramanian.S is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 182
Balasubramanian.S is on a distinguished road
Default Re: upload file

Hi Kirubha,

Hope this helps..

string connectionString = System.Configuration.ConfigurationManager.AppSetti ngs["connectionstring"];
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();

SqlCommand cmd1 = new SqlCommand("select img_data from images where img_no= 1" , conn);
byte[] img = (byte[])cmd1.ExecuteScalar();

SqlCommand cmd = new SqlCommand("select img_name, img_contenttype from images where img_no=1", conn);
SqlDataReader rdr = null;
rdr = cmd.ExecuteReader();
//byte[] img;
//Stream str;
while (rdr.Read())
{
imagename.Text = rdr.GetValue(0).ToString();
imagetype.Text = rdr.GetValue(1).ToString();
}

string savelocation = "D://App_Data//" +imagename.Text;
FileStream fs = new FileStream(savelocation, FileMode.Append, FileAccess.Write);
fs.Write(img, 0, img.Length);
Image1.Visible = true;
fs.Close();
conn.Close();
Image1.ImageUrl = savelocation;
__________________
S.Balasubramanian
Nothing is impossible
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I extract just the extension file name from a forms file upload field? itbarota HTML, CSS and Javascript Coding Techniques 1 10-22-2007 08:32 AM
File Upload Size Limit KiruthikaSambandam ASP and ASP.NET Programming 9 09-27-2007 03:16 AM
How to Upload a file using Ajax? ramkumaraol PHP Programming 1 08-06-2007 08:58 AM
Info: File Upload using Perl raj Perl 0 07-23-2007 06:00 AM
Creating a Multi-File Upload Script in PHP oxygen PHP Programming 0 07-20-2007 12:51 AM


All times are GMT -7. The time now is 07:12 PM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0