How to save Image to a specific folder?

Asked

Viewed 531 times

2

I have the following code:

protected void btnSalvarDestaque_Click(object sender, EventArgs e)
{
    if (fuFotos.HasFile)
    {
        string strname = fuFotos.FileName;
        fuFotos.PostedFile.SaveAs(Server.MapPath(".") + "//Content/Destaques//" + strname);
        string path = "~/Content/Destaques/" + strname.ToString();
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into Destaque values('" + txtTitulo.Text + "','" + txtValor.Text + "','" + strname + "')", con);
        cmd.ExecuteNonQuery();
        con.Close();
        lblDestaque.Text = "Destaque cadastrado com sucesso!";
        txtTitulo.Text = "";
        txtValor.Text = "";
    }
    else
    {
        lblDestaque.Text = "Por favor selecione uma foto!";
    }
}

The client fills in the fields: inserir a descrição da imagem aqui

The path is saved in the bank, and the photo would have to be added in the folder in question which is Featured: inserir a descrição da imagem aqui

What happens is that the image is saved right in the bank, but it is not being added in the folder and when the page is executed it appears only this:

inserir a descrição da imagem aqui

I’m not able to write the right way, can anyone help me? Thanks in advance! : D

  • Which path appears on the page?

1 answer

1


You almost got there, to fix just change the shape seguitne

protected void btnSalvarDestaque_Click(object sender, EventArgs e)
{
    if (fuFotos.HasFile)
    {
        string strname = fuFotos.FileName;
        string path = "~/Content/Destaques/" + strname.ToString();
        fuFotos.PostedFile.SaveAs(Server.MapPath(path));
        con.Open();
        SqlCommand cmd = new SqlCommand("insert into Destaque values('" + txtTitulo.Text + "','" + txtValor.Text + "','" + strname + "')", con);
        cmd.ExecuteNonQuery();
        con.Close();
        lblDestaque.Text = "Destaque cadastrado com sucesso!";
        txtTitulo.Text = "";
        txtValor.Text = "";
    }
    else
    {
        lblDestaque.Text = "Por favor selecione uma foto!";
    }
}
  • Thanks Brow, it worked right!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.