469,341 Members | 7,697 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,341 developers. It's quick & easy.

Problem with Image Uploading

39
Hi

Using FileUpload control to upload the image I have given the coding as
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Web;
  5. using System.Web.Security;
  6. using System.Web.UI;
  7. using System.Web.UI.WebControls;
  8. using System.Web.UI.WebControls.WebParts;
  9. using System.Web.UI.HtmlControls;
  10. using System.IO;
  11. using System.Data.SqlClient;
  12. using System.Net.Mail;
  13.  
  14.  
  15. public partial class _Default : System.Web.UI.Page
  16. {
  17.     protected void Page_Load(object sender, EventArgs e)
  18.     {
  19.  
  20.     }
  21.  
  22.     protected void Button1_Click1(object sender, EventArgs e)
  23.     {
  24.         string s = FileUpload1.FileName;
  25.         FileInfo ff = new FileInfo(s);
  26.         long num = ff.Length;
  27.         FileStream ft = new FileStream(s, FileMode.Open, FileAccess.Read);
  28.         BinaryReader br = new BinaryReader(ft);
  29.  
  30.         Byte[] data = br.ReadBytes((int)num);
  31.  
  32.         SqlConnection con = new SqlConnection("user id=sa;password=cast;database=Hello_Dr;server=AURORA-SERVER");
  33.         con.Open();
  34.         SqlCommand cmd = new SqlCommand("insert into imgdatastore values('" + data + "')", con);
  35.         cmd.ExecuteNonQuery();
  36.         Response.Redirect("Inserted");
  37.         con.Close();
  38.  
  39.     }
  40. }
  41.  
but while running, if I upload the jpg file named 'title_1.JPG'.

the error is

Could not find file 'title_1.JPG'.
May 30 '08 #1
2 1057
Hello,

FileUpload.FileName property - Gets the name of a file on a client to upload using the FileUpload control.

So in your case, You cannot use this property for initialize new instance of FileInfo class.
Try this code, I think it would be work properly:
Expand|Select|Wrap|Line Numbers
  1. protected void Button1_Click1(object sender, EventArgs e)
  2. {
  3.  
  4. if(FileUpload1.HasFile)
  5. {
  6. // get file name without path.
  7. string s = Path.GetFileName(FileUpload1.FileName);
  8.  
  9. int fileLen = FileUpload1.PostedFile.ContentLength;
  10. byte[] data = new byte[fileLen];
  11. data = FileUpload1.FileBytes;
  12.  
  13. SqlConnection con = new SqlConnection("user id=sa;password=cast;database=Hello_Dr;server=AUROR A-SERVER");
  14. con.Open();
  15. SqlCommand cmd = new SqlCommand("insert into imgdatastore values('" + data + "')", con);
  16. cmd.ExecuteNonQuery();
  17. Response.Redirect("Inserted");
  18. con.Close();
  19. }
  20. else
  21. {
  22.  // any message for user
  23. }
  24.  
  25. }
  26.  
Hope this helps.
May 30 '08 #2
suganya
39
Hello,

FileUpload.FileName property - Gets the name of a file on a client to upload using the FileUpload control.

So in your case, You cannot use this property for initialize new instance of FileInfo class.
Try this code, I think it would be work properly:
Expand|Select|Wrap|Line Numbers
  1. protected void Button1_Click1(object sender, EventArgs e)
  2. {
  3.  
  4. if(FileUpload1.HasFile)
  5. {
  6. // get file name without path.
  7. string s = Path.GetFileName(FileUpload1.FileName);
  8.  
  9. int fileLen = FileUpload1.PostedFile.ContentLength;
  10. byte[] data = new byte[fileLen];
  11. data = FileUpload1.FileBytes;
  12.  
  13. SqlConnection con = new SqlConnection("user id=sa;password=cast;database=Hello_Dr;server=AUROR A-SERVER");
  14. con.Open();
  15. SqlCommand cmd = new SqlCommand("insert into imgdatastore values('" + data + "')", con);
  16. cmd.ExecuteNonQuery();
  17. Response.Redirect("Inserted");
  18. con.Close();
  19. }
  20. else
  21. {
  22.  // any message for user
  23. }
  24.  
  25. }
  26.  
Hope this helps.

I modified the coding as above

But the Error is


The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
May 30 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

5 posts views Thread by ok | last post: by
2 posts views Thread by Faree | last post: by
3 posts views Thread by Pitcairnia | last post: by
8 posts views Thread by ctiggerf | last post: by
pezholio
1 post views Thread by pezholio | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
reply views Thread by Purva khokhar | last post: by
1 post views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.