Search code examples
c#downloadasyncfileupload

File downloading on postback


I have an anchor tag, an AsyncFileUpload control, and a span. The anchor tag's InnerText is set when a file exists in the database, if not, it is hidden. It also has a ServerClick event which downloads the file.

The span tag's InnerText displays the filename of the file uploaded using the AsyncFileUpload OnUploadedComplete.

When I click on the anchor, the file downloads (which is good.) But when I change the file (using the AsyncFileUpload) it posts back and the ServerClick method of the anchor tag is fired again therefore downloading the file again.

<a id="lnkDownloadFile" runat="server"></a>
<span id="spnFilename" runat="server"></span>
<input type="button" id="btnReplaceFile" value="Replace File" runat="server" />
<div>
    <ajaxToolkit:AsyncFileUpload ID="fuFile" runat="server" OnUploadedComplete="UploadComplete" OnClientUploadError="UploadError" />
</div>

Is there any way around this?

Thank you.


Solution

  • I am not sure why this is happening but one of the work-around could be to use hidden field to confirm if post-back has happened due to click on download link. For example,

    <input type="hidden" id="downloadFile" runat="server" />
    <asp:LinkButton id="lnkDownloadFile" runat="server" OnClientClick="return setDownloadFile();" />
    <script type="text/java-script" >
     function setDownloadFile() {
          document.getElementById('<%= downloadFile.ClientID>').value = 'true';
          return true;
     }
    </script>
    

    You can check downloadFile value in the link button click to decide whether to download file or not. Note that I have used LinkButton because I am sure about its client click attribute - you can try use click attribute with html anchor (what I am not 100% sure if it would interfare with ServerClick event handler).