0

I am using FileUpload to upload a file. Once the file has been uploaded, I would like the user to see a 'Remove' link that will delete the file from the server.

How would I make such a link visible only after the file has been completely uploaded?

Niloo
  • 1,205
  • 5
  • 29
  • 53

1 Answers1

1

I have tried using data controls to achieve this.

 <asp:Panel ID="pnlUpload" runat="server">
    <asp:FileUpload ID="fup1" runat="server" ClientIDMode="Static" />
    <asp:Button ID="btnServer" runat="server" Text="Submit" OnClick="btnSubmit_Click"
        ClientIDMode="Static" CssClass="none" />
</asp:Panel>
<asp:Repeater ID="rptUploads" runat="server" OnItemCommand="rptUploads_ItemCommand">
    <HeaderTemplate>
        <table width="100%">
            <th>
                File Name
            </th>
            <th>
                Actions
            </th>
    </HeaderTemplate>
    <ItemTemplate>
        <tr>
            <td>
                <%# Container.DataItem %>
            </td>
            <td>
                <asp:LinkButton ID="LinkButton1" runat="server" Text="Remove" CommandName="Delete"
                    CommandArgument="<%# Container.DataItem %>"></asp:LinkButton>
            </td>
        </tr>
    </ItemTemplate>
    <FooterTemplate>
        </table>
    </FooterTemplate>
</asp:Repeater>
<style>
    .none
    {
        display: none;
    }
</style>
<script type="text/javascript">
    (function () {
        if (document.getElementById('fup1') != null) {
            document.getElementById('fup1').onchange = function () { document.getElementById('btnServer').click(); };
        }
    })();
</script>

And code behind

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using System.IO;

public partial class _Default : System.Web.UI.Page
{
    public List<string> Files
    {
        get
        {
            if (ViewState["files"] == null)
            {
                ViewState["files"] = new List<string>();
            }
            return (List<string>)ViewState["files"];
        }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        if (fup1.HasFile && !Files.Contains(fup1.FileName))
        {
            fup1.SaveAs(Server.MapPath("~/App_Data/") + fup1.FileName);
            Files.Add(fup1.FileName);
            rptUploads.Visible = true;
            rptUploads.DataSource = Files;
            rptUploads.DataBind();
            pnlUpload.Visible = false;
        }
    }

    protected void rptUploads_ItemCommand(object sender, RepeaterCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            var filename = Server.MapPath("~/App_Data/") + e.CommandArgument;
            if (File.Exists(filename))
            {
                File.Delete(filename);
                Files.Remove(e.CommandArgument.ToString());               
                rptUploads.Visible = false;
                pnlUpload.Visible = true;
            }
        }
    }
}
Sunny
  • 4,765
  • 5
  • 37
  • 72
  • Thanks a lot, i want to add one file with fileupload. – Niloo Dec 15 '12 at 04:56
  • You mean for 1 file, it will be too much using data control?.FYI, I also tried using dynamic controls but code is little more clumsy than this so preferred this way... – Sunny Dec 15 '12 at 05:27
  • I want, user don't add file if fileupload has file. mean: fileupload is disable when a file selected. – Niloo Dec 15 '12 at 07:07
  • Check updated code. Wrapped upload controls into a panel and set visible = false. – Sunny Dec 15 '12 at 07:40
  • Thanks a lot, but a question:Is there no way to understand the file upload is complete without click submit button??!!! – Niloo Dec 15 '12 at 08:08
  • Hide the submit button and there is onchange client event for file upload. In this event, raise the click event of button. More info, check this out...http://stackoverflow.com/questions/5942821/how-to-fire-event-on-file-select – Sunny Dec 15 '12 at 08:18
  • If possible please give further explained...:) – Niloo Dec 15 '12 at 09:01
  • If it answered your question, would you mind marking it as answer. – Sunny Dec 17 '12 at 15:44