Problem with Image Uploading

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • suganya
    New Member
    • Dec 2006
    • 39

    Problem with Image Uploading

    Hi

    Using FileUpload control to upload the image I have given the coding as
    [code=cpp]
    using System;
    using System.Data;
    using System.Configur ation;
    using System.Web;
    using System.Web.Secu rity;
    using System.Web.UI;
    using System.Web.UI.W ebControls;
    using System.Web.UI.W ebControls.WebP arts;
    using System.Web.UI.H tmlControls;
    using System.IO;
    using System.Data.Sql Client;
    using System.Net.Mail ;


    public partial class _Default : System.Web.UI.P age
    {
    protected void Page_Load(objec t sender, EventArgs e)
    {

    }

    protected void Button1_Click1( object sender, EventArgs e)
    {
    string s = FileUpload1.Fil eName;
    FileInfo ff = new FileInfo(s);
    long num = ff.Length;
    FileStream ft = new FileStream(s, FileMode.Open, FileAccess.Read );
    BinaryReader br = new BinaryReader(ft );

    Byte[] data = br.ReadBytes((i nt)num);

    SqlConnection con = new SqlConnection(" user id=sa;password= cast;database=H ello_Dr;server= AURORA-SERVER");
    con.Open();
    SqlCommand cmd = new SqlCommand("ins ert into imgdatastore values('" + data + "')", con);
    cmd.ExecuteNonQ uery();
    Response.Redire ct("Inserted") ;
    con.Close();

    }
    }
    [/code]

    but while running, if I upload the jpg file named 'title_1.JPG'.

    the error is

    Could not find file 'title_1.JPG'.
    Last edited by Frinavale; May 30 '08, 02:21 PM. Reason: added code tags
  • GregoryPankov
    New Member
    • May 2008
    • 6

    #2
    Hello,

    FileUpload.File Name 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:
    Code:
    protected void Button1_Click1(object sender, EventArgs e)
    {
    
    if(FileUpload1.HasFile)
    {
    // get file name without path.
    string s = Path.GetFileName(FileUpload1.FileName);
    
    int fileLen = FileUpload1.PostedFile.ContentLength;
    byte[] data = new byte[fileLen];
    data = FileUpload1.FileBytes;
    
    SqlConnection con = new SqlConnection("user id=sa;password=cast;database=Hello_Dr;server=AUROR A-SERVER");
    con.Open();
    SqlCommand cmd = new SqlCommand("insert into imgdatastore values('" + data + "')", con);
    cmd.ExecuteNonQuery();
    Response.Redirect("Inserted");
    con.Close();
    }
    else
    {
     // any message for user
    }
    
    }
    Hope this helps.

    Comment

    • suganya
      New Member
      • Dec 2006
      • 39

      #3
      Originally posted by GregoryPankov
      Hello,

      FileUpload.File Name 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:
      Code:
      protected void Button1_Click1(object sender, EventArgs e)
      {
      
      if(FileUpload1.HasFile)
      {
      // get file name without path.
      string s = Path.GetFileName(FileUpload1.FileName);
      
      int fileLen = FileUpload1.PostedFile.ContentLength;
      byte[] data = new byte[fileLen];
      data = FileUpload1.FileBytes;
      
      SqlConnection con = new SqlConnection("user id=sa;password=cast;database=Hello_Dr;server=AUROR A-SERVER");
      con.Open();
      SqlCommand cmd = new SqlCommand("insert into imgdatastore values('" + data + "')", con);
      cmd.ExecuteNonQuery();
      Response.Redirect("Inserted");
      con.Close();
      }
      else
      {
       // any message for user
      }
      
      }
      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.

      Comment

      Working...