How to clear text box records before or after downloading a file in ASP.NET web application

Roefatin, Ridzuan 40 Reputation points
2023-01-26T04:58:36.0633333+00:00
     using (FileStream fs = File.Create(dataDir + xmlFileToConvertTo))
                            {
                                Byte[] info = new UTF8Encoding(true).GetBytes(xmlData);
                                fs.Write(info, 0, info.Length);
                            }
							    string message = $"{xmlFileToConvertTo} has been stored in {dataDir1}";
                                ScriptManager.RegisterClientScriptBlock(Page, typeof(Page), "ClientScript", "alert('"+message+"')", true);
                                txtPartNo.Text = string.Empty;
							    DdStrand.Text = string.Empty;
							    string _path = @"\\server\recipe\" + xmlFileToConvertTo;
							    System.IO.FileInfo _file = new System.IO.FileInfo(_path);
							if (_file.Exists)
							{
								    Response.Clear();
								    Response.AddHeader("Content-Disposition", "attachment; filename=" + _file.Name);
								    Response.AddHeader("Content-Length", _file.Length.ToString());
								    Response.ContentType = "application/octet-stream";
								    Response.WriteFile(_file.FullName);
								    Response.End();
                                    txtPartNo.Text = null;
                                    DdStrand.Text = null;

							}
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,160 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
9,919 questions
{count} votes

Accepted answer
  1. XuDong Peng-MSFT 8,096 Reputation points Microsoft Vendor
    2023-01-26T07:55:16.7066667+00:00

    Hi @Roefatin, Ridzuan,

    Well, what I think you need to understand is this: when you do a download operation, the respond to browser is file, not the page. So if you want to implement such a requirement, you can try to use javascript, something like this:

    <body>
        <form id="form1" runat="server">
                <asp:ScriptManager runat="server"></asp:ScriptManager>
                <asp:FileUpload ID="_File" runat="server" />
                <asp:TextBox runat="server" ID="txtPartNo"></asp:TextBox>
                <asp:Button ID="btnDownload" runat="server" Text="Download File" 
                OnClick="btnDownload_Click"/>
            
            <script src="Scripts/jquery-3.4.1.min.js"></script>
            <script>
            $(function () {
                $("#<%= btnDownload.ClientID %>").click(function () {
                    debugger;
                    $("#<%= txtPartNo.ClientID %>").val("");
                });
            });
            </script>
        </form>
    </body>
    

    And the button click event:

    
            protected void btnDownload_Click(object sender, EventArgs e)
            {
                if (_File.HasFile)
                {
                    txtPartNo.Text = null;
                    Response.Clear();
                    Response.AddHeader("Content-Disposition", "attachment; filename=" + _File.FileName);
                    Response.AddHeader("Content-Length", _File.FileContent.Length.ToString());
                    Response.ContentType = "application/octet-stream";
                    Response.WriteFile("background.jpg");
                    Response.End();
                    //txtPartNo.Text = null;
                }
            }
    

    Result: enter image description here

    Best regards,

    Xudong Peng


    If the answer is the right solution, please click "Accept Answer" and kindly upvote. If you have extra questions about this answer, please click "Comment". Note: Please follow the steps in our [documentation][1] to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful