简体   繁体   中英

Checking Status Code without Disrupting Download Processing

I'm attempting to download a file via a hidden iframe element. I'd also like to check the response for an error code when it comes back, for the purposes of displaying an error message if the file preparation fails. The below code is what I have right now.

var srcUrl = "/MyProject/servlet?action=getFile&output=STREAM&asAttachment=Y&param1=userinput1&param2=userinput2

$("#downloadFrame").load(
    srcUrl,
    function(response, status, xhr) {
        if (xhr.status == 204) {
            $("#errorDiv").html(xhr.statusText);
        }
    }
);

When the response comes back with the error code, it displays the expected error message. However, when the file is prepared and return correctly, it doesn't save the byte stream that comes back. This code will download the file, but it also doesn't check for a status code.

 $("#downloadFrame").attr("srcUrl", params);

Possibly of interest, this is a jsp page making a request to a java servlet running on Tomcat 6.0.

Tried adding an action to the success of the download?

        let srcUrl = "/MyProject/servlet?action=getFile&output=STREAM&asAttachment=Y&param1=userinput1&param2=userinput2

        $("#downloadFrame").load(
            srcUrl,
            function(response, status, xhr) {
                if (xhr.status == 204) {
                    $("#errorDiv").html(xhr.statusText);
                }
                if (xhr.status == 200 & xhr.readyState == 4) {
                    $("#successDiv").html(xhr.statusText); //xhr.(what you need to save)
                }
            }
        );

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM