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. ...
| |||||||
| Register | FAQ | Members List | Calendar | Mark Forums Read |
| |||
| 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 |
| |||
| 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. |
| |||
| 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 |
![]() |
| Thread Tools | |
| Display Modes | |
| |
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 |