Good day. I have this question.

On asp.net there is a HttpHandler button in PostBackUrl

 <asp:Button ID="Button1" PostBackUrl ="/handler.axd?qsd=10" runat="server" Text="Button" /> 

This handler, by clicking on the button, returns a file from the database. The URL of the page remains the same, for example http://localhost:56278/Default . But there are cases when it returns nothing and in this case the URL becomes http://localhost:56278/handler.axd . How, in case there is nothing to return, return something of type alert (); without updating the page and changing its URL?

An example of my handler:

  public class Document: IHttpHandler { public void ProcessRequest(HttpContext context) { HttpRequest Request = context.Request; HttpResponse Response = context.Response; Response.AddHeader("Content-Disposition", "attachment; filename=somefile.ext"); Response.ContentType = "application/javascript"; Response.Write("<script type ='text/javascript'>alert('Error!');</script>"); } public bool IsReusable { get { return false; } } } 

    1 answer 1

    The problem is not that "javascript cannot be returned". The problem is that the browser does not expect it with your work scheme.

    There are two options for performing a POST request:

    1. usual fullscreen post. It is done using a form ( <form method='post'> ) and sending it to the server by a script or by clicking on <input type='submit'> .
      • If at the same time the Content-Disposition response came from the server, then the current page remains live, and the result is saved as a file.
      • If without Content-Disposition - the answer is considered a successful transition, the current page is thrown out, the content of the response is shown in the browser window, the window url changes to the one for which the request was made
    2. AJAX is a special call from a script, previously implemented via the XMLHttpRequest object (added by Microsoft for Outlook Web, promoted by Google for GMail), and before it via hacks like download behavior. Now - with built-in browser + wrappers for them of the form $.post(url) . This method
      • always triggered from javascript .
      • receives a response from the server as a JavaScript object — practically, as a string.

    This line can be shown on the page, executed as a script (if an alert has arrived in it, for example). But in general it is impossible to give it out as a downloadable file (it is possible in html 5 via the fileSystem API).

    You have the first option. As soon as the request is sent, you cannot “change your mind” and change it to the second one, especially from the server side. The maximum that can be done in your case is to divide the method into two:

    • The first is to call via ajax and return json in it with an action - either "download file + link to the second method", or "show an error".
    • The second one is to pull the full-screen post and always give the file over it (you already have it). If something went wrong - fall.
    • What is meant by "pull fullscreen post"? Normal postback method on server side? That's how it all works now, there are just handlers in some places, and postbacks in some places. And I thought to bring all a single mind. - koks_rs
    • The @koks_rs normal fullscreen post is a form on the client and the submit button in it (or sending the same form as a script). It doesn't matter what handles this POST on the server side - postback method, handler, mvc or php in general. It is important that this is not ajax, but simply sending the form. And in response to it nothing can be done at all - because from the point of view of the browser it is a transition from the current page to somewhere else. And as soon as the request went to the server, the current page no longer has anything to do with it. - PashaPash ♦
    • @koks_rs i.e. you have the "URL lags unchanged" and the page is not visible because "the page has loaded the file." And because the browser receives in response a file that it saves to disk. And instead of the old page, he has nothing to show, so he remains on it. It would be that - he would have left her the same way. This can be seen, for example, in IE with ctrl + click on the file upload link. He opens a new tab, cheerfully so is going to show the page - and immediately receives a file, changes his mind, closes the tab and starts downloading the file. - PashaPash ♦
    • Thanks for the detailed explanation. - koks_rs