无法按名称或路径

时间:2018-04-22 18:16:09

标签: c# mysql asp.net

我正在制作一个GridView CRUD,可以在数据库中保存:关系 FileName FilePath (所有这些都是varchar),以及网页内文件夹中的图像。

我已经可以添加,选择和删除了,但是当我尝试更新时出现问题。当我尝试更新时,我成功修改了数据库,因为它只是文本。但是,当新图片上传时,旧图片仍然存在,我想在更新时删除旧图片,以便新图片保留在文件/文件夹中“图像”,我应该尝试什么?

enter image description here

(更新后图片仍然存在) enter image description here

这是我的代码,删除旧图像:

BackEnd(只是更新方法)

protected void gvImages_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    HttpPostedFile file = ((FileUpload)gvImages.Rows[e.RowIndex].FindControl("FileUpload2")).PostedFile;
    string fileName = Path.GetFileName(file.FileName);

    MySqlConnection con = new MySqlConnection(strConnString);
    string strQuery = "update testingdb.country set Relation=@Relation, FileName=@FileName, FilePath=@FilePath where idcountry=@idcountry";
    MySqlCommand cmd = new MySqlCommand(strQuery);
    //string oldFileName = gvImages.Rows[e.RowIndex].Cells[2].Text;
    if (File.Exists(Server.MapPath("images/") + fileName))
    {
        lblFail.Visible = true;
        lblFail.Text = "Ya existe esta entrada.";
        TextBox1.Text = "";
    }
    else
    {

       //File.Delete(Server.MapPath("images/") + oldFileName);
        //guarda archivos al disco
        file.SaveAs(Server.MapPath("images/" + fileName));

        cmd.Connection = con;
        cmd.Parameters.AddWithValue("@Relation", (gvImages.Rows[e.RowIndex].FindControl("txtRel") as TextBox).Text.Trim());
        cmd.Parameters.AddWithValue("@FileName", fileName);
        cmd.Parameters.AddWithValue("@FilePath", "images/" + fileName);
        cmd.Parameters.AddWithValue("@idcountry", Convert.ToInt32(gvImages.DataKeys[e.RowIndex].Value.ToString()));
        cmd.CommandType = CommandType.Text;
        try
        {
            con.Open();
            cmd.ExecuteNonQuery();
            gvImages.EditIndex = -1;
        }
        catch (Exception ex)
        {
            lblFail.Visible = true;
            lblFail.Text = ex.Message;
        }
        finally
        {
            con.Close();
            con.Dispose();

        }
        this.MostrarImagen();
        lblSuccess.Visible = true;
        lblSuccess.Text = "Exito al editar.";
    }
}

FontEnd(只是Gridview)

        <asp:Label runat="server" ID="lblSuccess" Text="" ForeColor="Green" Visible="false"></asp:Label> 

        <asp:Label runat="server" ID="lblFail" Text="" ForeColor="Red" Visible="false"></asp:Label> 

        <asp:GridView EmptyDataText="No hay registros en la base de datos!" ID="gvImages" runat="server" DataKeyNames="idcountry" OnRowCommand="gvImages_RowCommand" AutoGenerateColumns="false" OnRowDataBound="gvImages_RowDataBound" Height="300px" OnRowEditing="gvImages_RowEditing" OnRowCancelingEdit="gvImages_RowCancelingEdit" OnRowUpdating="gvImages_RowUpdating">
            <Columns>
                <asp:BoundField DataField="idcountry" HeaderText="ID" ReadOnly="true" />
                <asp:TemplateField HeaderText="Relacion">
                    <ItemTemplate>
                        <asp:Label Text='<%# Eval("Relation") %>' runat="server"></asp:Label>
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:TextBox runat="server" ID="txtRel" Text='<%# Eval("Relation") %>'></asp:TextBox> />
                    </EditItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField HeaderText="Nombre">
                    <ItemTemplate>
                        <asp:Label ID="lblNombre2" Text='<%# Eval("FileName") %>' runat="server"></asp:Label>
                    </ItemTemplate>

                </asp:TemplateField>

                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:Image ID="Image1" runat="server" ImageUrl='<%# Eval("FilePath") %>' Height="300" Width="300" />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:FileUpload ID="FileUpload2" runat="server" />
                    </EditItemTemplate>
                </asp:TemplateField>

                <asp:TemplateField>
                    <ItemTemplate>
                        <asp:ImageButton CausesValidation="false" CssClass="btnOPTIONS" runat="server" ImageUrl="~/imgBTN/edit.png" Width="30px" Height="30px" ToolTip="Edit" CommandName="Edit" />
                        <asp:ImageButton CausesValidation="false" runat="server" ImageUrl="~/imgBTN/delete.png" Width="30px" Height="30px" ToolTip="Delete" CommandName="Delete" />
                    </ItemTemplate>
                    <EditItemTemplate>
                        <asp:ImageButton CausesValidation="false" CssClass="btnOPTIONS" runat="server" ImageUrl="~/imgBTN/save.png" Width="30px" Height="30px" ToolTip="Update" CommandName="Update" />
                        <asp:ImageButton CausesValidation="false" runat="server" ImageUrl="~/imgBTN/cancel.png" Width="30px" Height="30px" ToolTip="Cancel" CommandName="Cancel" />
                    </EditItemTemplate>
                </asp:TemplateField>
            </Columns>
        </asp:GridView>

我尝试使用这段代码,但它不起作用(这在显示的代码中):

//string oldFileName = gvImages.Rows[e.RowIndex].Cells[2].Text;

//File.Delete(Server.MapPath("images/") + oldFileName);

此外,我的删除方法不会从数据库中删除“images”文件夹中的图像。

  

编辑:当我取消注释这些代码行时会发生这种情况:   enter image description here

1 个答案:

答案 0 :(得分:0)

我设法解决了这个问题,我所要做的就是将oldFileName更改为:

    string oldFileName = (gvImages.Rows[e.RowIndex].FindControl("lblNombre2") as Label).Text;

选择编辑事件之前标签中的数据。

还有:

中加入:

string mypath = Server.MapPath("images/") + oldFileName;
        File.SetAttributes(mypath, FileAttributes.Normal);
        File.Delete(mypath);

在连接结束时。

最后......ಥvಥ