By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,694 Members | 1,294 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,694 IT Pros & Developers. It's quick & easy.

Problem with Image Uploading

P: 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
Share this Question
Share on Google+
2 Replies


P: 6
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

P: 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.