How to save Image to a specific folder?

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 customer fills in the fields:

Thepathissavedtothebank,andthephotowouldhavetobeaddedtothefolderinquestionthatisHighlight:

Whathappensisthattheimagesavesrightonthebank,butitisnotbeingaddedtothefolderandwhenthepageisexecuteditonlyappears:

I do not know how to write the right way, can anyone help me? Thank you! : D

    
asked by anonymous 28.09.2016 / 04:31

1 answer

1

You almost got there, to correct just change the way you go

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!";
    }
}
    
28.09.2016 / 14:55