0

我在我的网络表单中使用了 3 个文件上传控件,在这里我给出了它的截图。 在此处输入图像描述

这是我的 aspx.cs 文件代码,

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BusinessLayer;
using System.IO;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;

namespace EventManagerApp.SuperAdmin
{
    public partial class Settings : System.Web.UI.Page
    {
        public string FilePath;
        protected void Page_Load(object sender, EventArgs e)
        {


        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {

            General_SettingsBL gb = new General_SettingsBL();


            gb.Privacy_Policies = System.IO.Path.GetExtension(fuprivacypolicies.FileName);



            gb.Privacy_Policies = System.IO.Path.GetExtension(fuprivacypolicies.FileName);
             gb.Refund_Policies = System.IO.Path.GetExtension(furefundpolicies.FileName);
            gb.Terms = System.IO.Path.GetExtension(futerms.FileName);
            gb.Trial_Period = decimal.Parse(txttrailperoid.Text);

            gb.Insert();
            lblmsg.Visible = true;
            lblmsg.Text = "Records Inserted Successfully";

        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {

        }

        protected void deleterefundpolicies(object sender, EventArgs e)
        {

            furefundpolicies.ID = null;


        protected void deleteprivacypolicies(object sender, EventArgs e)
        {
            fuprivacypolicies.ID = null;
            // fuprivacypolicies = new FileUpload();
        }

        protected void deleteterms(object sender, EventArgs e)
        {
          futerms.ID = null;
            // futerms = new FileUpload();
        }



    }
}

我将此代码用于 .aspx 文件,

     <%@ Page Title="" Language="C#" MasterPageFile="~/SuperAdmin/EventManager_SuperAdmin.Master" AutoEventWireup="false" CodeBehind="Settings.aspx.cs" Inherits="EventManagerApp.SuperAdmin.Settings" %>
<%@ Register TagPrefix="asp" Namespace="AjaxControlToolkit" Assembly="AjaxControlToolkit" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">

</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
  <div class="container">
        <div class="row">
            <h3>
                Settings</h3>
            <hr>
        </div>
        <div class="row">
            <div class="col-md-6">
                <form class="form-horizontal" role="form">
                <div class="form-group">
                    <label for="inputEmail1" class="col-md-4 control-label">
                        Refund Policies:  </label>
                    <div class="col-md-6">
                         <asp:FileUpload ID="furefundpolicies"   runat="server" />

                    </div>
                   <div class="col-md-6">
                   <asp:Button ID="btnrefunfpolicy" runat="server" Text="DELETE" class="btn btn-default" OnClick="deleterefundpolicies" />
                    </div>
                </div>

                <div class="form-group">
                    <label for="inputEmail1" class="col-md-4 control-label">
                         Privacy Policies</label>
                    <div class="col-md-6">
                         <asp:FileUpload ID="fuprivacypolicies"   runat="server" />
                    </div>
                      <div class="col-md-6">
                   <asp:Button ID="btnprivacypolicy" runat="server" Text="DELETE" class="btn btn-default" OnClick="deleteprivacypolicies" />
                    </div>
                </div>
                <div class="form-group">
                    <label for="inputEmail1" class="col-md-4 control-label">
                         Terms</label>
                    <div class="col-md-6">
                     <asp:FileUpload ID="futerms"   runat="server" />
                    </div>
                       <div class="col-md-6">
                   <asp:Button ID="btnterms" runat="server" Text="DELETE" class="btn btn-default" OnClick="deleteterms" />
                    </div>
                </div>

                <div class="form-group">
                    <label for="inputEmail1" class="col-md-4 control-label">
                         Trial Period</label>
                    <div class="col-md-6">

                       <asp:TextBox ID="txttrailperoid" runat="server" class="form-control"></asp:TextBox>
                      <%--  <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txttrailperoid"
                            Display="Dynamic" ErrorMessage="Please Enter Value"></asp:RequiredFieldValidator>--%>
                    </div>
                </div>
                <div class="col-md-10">
                    <asp:Label ID="lblmsg" runat="server" Visible="false" class="alert alert-success form-group"></asp:Label>
                </div>
                <div class="form-group">
                    <label for="inputEmail1" class="col-md-4 control-label">
                        &nbsp;</label>
                    <div class="col-md-6">
                        <asp:Button ID="btnSubmit" runat="server" Text="SAVE" class="btn btn-default" OnClick="btnSubmit_Click" />
                        <asp:Button ID="btnCancel" runat="server" Text="CANCEL" CausesValidation="false"
                            class="btn btn-default" OnClick="btnCancel_Click" />
                    </div>
                </div>
                </form>
            </div>
        </div>
    </div>
</asp:Content>

在我的屏幕截图中,我描述了我的问题,我只想删除一个上传的文件。

4

1 回答 1

1

看,您的代码并没有保存所有三个文件!您只保存其中一个,然后将其删除(或不删除)!正如我所承诺的,如果这可以帮助您,我想向您展示另一种方法。

给你: - 我在你的 aspx 中添加了三个标签 - 删除代码隐藏文件中的注释以执行你的 gb 操作......

<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server"><div class="container">
        <div class="row">
        <h3>
                Settings</h3>
        <hr>
    </div>
    <div class="row">
        <div class="col-md-6">
            <form class="form-horizontal" role="form">
            <div class="form-group">
                <label for="inputEmail1" class="col-md-4 control-label">
                    Refund Policies:  </label>
                <div class="col-md-6">
                     <asp:FileUpload ID="furefundpolicies"   runat="server" />

                </div>
               <div class="col-md-6">
                   <asp:Label ID="Label1" runat="server" Text="No file yet, Please Click Upload before using DELETE button"></asp:Label><br />
               <asp:Button ID="btnrefunfpolicy" runat="server" Text="DELETE" class="btn btn-default" OnClick="deleterefundpolicies" />
                </div>
            </div>

            <div class="form-group">
                <label for="inputEmail1" class="col-md-4 control-label">
                     Privacy Policies</label>
                <div class="col-md-6">
                     <asp:FileUpload ID="fuprivacypolicies"   runat="server" />
                </div>
                  <div class="col-md-6">
                   <asp:Label ID="Label2" runat="server" Text="No file yet, Please Click Upload before using DELETE button"></asp:Label><br />
               <asp:Button ID="btnprivacypolicy" runat="server" Text="DELETE" class="btn btn-default" OnClick="deleteprivacypolicies" />
                </div>
            </div>
            <div class="form-group">
                <label for="inputEmail1" class="col-md-4 control-label">
                     Terms</label>
                <div class="col-md-6">
                 <asp:FileUpload ID="futerms"   runat="server" />
                </div>
                   <div class="col-md-6">
                   <asp:Label ID="Label3" runat="server" Text="No file yet, Please Click Upload before using DELETE button"></asp:Label><br />
               <asp:Button ID="btnterms" runat="server" Text="DELETE" class="btn btn-default" OnClick="deleteterms" />
                </div>
            </div>

            <div class="form-group">
                <label for="inputEmail1" class="col-md-4 control-label">
                     Trial Period</label>
                <div class="col-md-6">

                   <asp:TextBox ID="txttrailperoid" runat="server" class="form-control"></asp:TextBox>
                  <%--  <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txttrailperoid"
                        Display="Dynamic" ErrorMessage="Please Enter Value"></asp:RequiredFieldValidator>--%>
                </div>
            </div>
            <div class="col-md-10">
                <asp:Label ID="lblmsg" runat="server" Visible="false" class="alert alert-success form-group"></asp:Label>
            </div>
            <div class="form-group">
                <label for="inputEmail1" class="col-md-4 control-label">
                    &nbsp;</label>
                <div class="col-md-6">
                    <asp:Button ID="btnSubmit" runat="server" Text="SAVE" class="btn btn-default" OnClick="btnSubmit_Click" />
                    <asp:Button ID="btnCancel" runat="server" Text="CANCEL" CausesValidation="false"
                        class="btn btn-default" OnClick="btnCancel_Click" />
                </div>
            </div>
            </form>
        </div>
    </div>
</div>

现在你有你的代码

    public string FilePath;
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {

        //General_SettingsBL gb = new General_SettingsBL();

        string fn = Path.GetFileName(fuprivacypolicies.PostedFile.FileName);
        string SaveLocation = Server.MapPath("UploadedCSVFiles") + "\\" + fn;
        FileInfo fi = new FileInfo(SaveLocation);


        fuprivacypolicies.PostedFile.SaveAs(SaveLocation);
        fuprivacypolicies.PostedFile.InputStream.Dispose();
        Session["fuprivacypoliciesPath"] = SaveLocation;
        Label2.Text = "Uploaded file : "+fn;


       // gb.Privacy_Policies = System.IO.Path.GetExtension(fuprivacypolicies.FileName);



       // gb.Privacy_Policies = System.IO.Path.GetExtension(fuprivacypolicies.FileName);
       // gb.Refund_Policies = System.IO.Path.GetExtension(furefundpolicies.FileName);
       // gb.Terms = System.IO.Path.GetExtension(futerms.FileName);
       // gb.Trial_Period = decimal.Parse(txttrailperoid.Text);

       // gb.Insert();
        lblmsg.Visible = true;
        lblmsg.Text = "Records Inserted Successfully";
        Label1.Text = "Uploaded file : " + fn;



        string ff = Path.GetFileName(furefundpolicies.PostedFile.FileName);
        string SaveLocation2 = Server.MapPath("UploadedCSVFiles") + "\\" + ff;


        furefundpolicies.PostedFile.SaveAs(SaveLocation2);
        furefundpolicies.PostedFile.InputStream.Dispose();
        Session["furefundpoliciesPath"] = SaveLocation2;
        Label2.Text = "Uploaded file : " + ff;




        string ft = Path.GetFileName(futerms.PostedFile.FileName);
        string SaveLocation3 = Server.MapPath("UploadedCSVFiles") + "\\" + ft;


        futerms.PostedFile.SaveAs(SaveLocation3);
        futerms.PostedFile.InputStream.Dispose();
        Session["futermsPath"] = SaveLocation3;
        Label3.Text = "Uploaded file : " + ft;




    }

    protected void btnCancel_Click(object sender, EventArgs e)
    {

    }

    protected void deleterefundpolicies(object sender, EventArgs e)
    {
        //string fn = Path.GetFileName(fuprivacypolicies.PostedFile.FileName);
        //string SaveLocation = Server.MapPath("UploadedCSVFiles") + "\\" + fn;
        //FileInfo fi = new FileInfo(SaveLocation);
        ////furefundpolicies.ID = null;
        //fi.Delete();
        if (Session["furefundpoliciesPath"] != null && File.Exists(Session["furefundpoliciesPath"].ToString()))
        {
            File.Delete(Session["furefundpoliciesPath"].ToString());

            Label1.Text = "File deleted";
        }

    }

    protected void deleteprivacypolicies(object sender, EventArgs e)
    {
        //fuprivacypolicies.ID = null;
        // fuprivacypolicies = new FileUpload();
        if (Session["fuprivacypoliciesPath"]!=null&&File.Exists(Session["fuprivacypoliciesPath"].ToString())) {
            File.Delete(Session["fuprivacypoliciesPath"].ToString());

            Label2.Text = "File deleted";
        }
    }

    protected void deleteterms(object sender, EventArgs e)
    {
        //futerms.ID = null;
        // futerms = new FileUpload();
        if (Session["futermsPath"]!=null&&File.Exists(Session["futermsPath"].ToString()))
        {
            File.Delete(Session["futermsPath"].ToString());
            Label3.Text = "File deleted";
        }
    }

这样,文件将根据单击的按钮单独删除!试试看

于 2013-10-24T12:26:00.213 回答