It is very common to use the Response stream to manage file download process. For example, the following code snippet shows how to use the Response stream to download any file:
The above code will work fine in most cases, however, if the Download button is in a UpdatePanel, then it will throw an AJAX error like this:
The error message tells the cause the error: you cannot use Response.Write in the UpdatePanel, so how to avoid this error or how to use Response stream to enable file download in UpdatePanel?
Actually, the real cause is that when an asynchronous postback is generated, the Response stream can’t be modified. In another work, if the download button generates a full postback, then there should be no problem with the above code. So what we need to do is to add a PostBackTrigger to the UpdatePanel and set the trigger’s ControlID to the ID of the button that will generate a full postback, in our example above, it will be ControlID=”btnDownload”.
Now if we run the above code again, a standard file download dialog will appear when the download button is clicked.
Hope it helps.