You must Sign In to post a response.
  • Category: ASP.NET

    Upload image with user name in asp.net with sql server

    hello sir.
    i have one webpage and i want to upload image ,
    if i upload any image there are save image name by default image name ..
    i want to change image name after upload any image (like user name or ID)
    please

    //sample :-

    string path = Server.MapPath("upload_image/");

    if (FileUpload1.HasFile)
    {
    string ext = Path.GetExtension(FileUpload1.FileName);

    if (ext == ".jpg" || ext == ".png")
    {
    FileUpload1.SaveAs(path + FileUpload1.FileName);
    string name = "upload_image/" + FileUpload1.FileName;
    string ss = "insert into upload (name,mobile,photo) values (N'" + TextBox1.Text + "',N'" + TextBox2.Text + "', N'" + name + "')";


    SqlCommand cmd = new SqlCommand(ss, con);
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();

    Label1.Visible = true;
    Label1.Text = "Upload image successfully";

    // display image
    SqlDataAdapter da = new SqlDataAdapter("select * from upload", con);
    DataTable dt = new DataTable();
    da.Fill(dt);
    Image2.ImageUrl = "upload_image/" + FileUpload1.FileName;
    Image2.Visible = true;
    // end display imagae
    DataBind();

    }
    else
    {
    Label1.Visible = true;
    Label1.Text = "You have to upload jpg or png file only !";
    }

    }
    else
    {
    Label1.Visible = true;

    Label1.Text = "Please Select File";

    }
    //end sample
  • #768249
    Hi,
    You are storing files extension in variable 'ext' so it is very easy. Just change your image save code line as follows:

    \\ FileUpload1.SaveAs(path + FileUpload1.FileName);
    FileUpload1.SaveAs(path + "YourName"+ ext);

    here file will be saved as YourName.jpg Or YourName.png depending on your selection.


  • Sign In to post your comments