How do you send a message to the client when PHP form submission download fails

jprice92 :

My app has the ability to request and download a multitude of different files and types from the database.

The way I'm currently doing this is with a hidden form input that will submit the request to server which then does the processing and downloads the file. The code for that looks like this:

       var action = '/download/downloader.php?actionname&filename=' + downloadfilename;
        
        var A = document.createElement("div");
        A.style.display = "none";
        document.body.appendChild(A);
        var formKey = 'someid';
        A.innerHTML = '<form id="' + formKey + '" method="post" action="' + action + '" accept-charset="utf-8"  enctype="application/x-www-form-urlencoded"' + target + '><input type="hidden" name="segmentno" id="segmentno"/> </form>';
        document.getElementById(formKey).submit();
        A.parentNode.removeChild(A);

The PHP side for downloading an XML file will look something like this (all the cases set the output data and headers accordingly)

// $a is the data from the database
        $xmlEncode = new XMLEncode();
        $xmlstring =  $xmlEncode->XML_SerializeData($a);
        $xmlstring = str_replace('&apos;', '&apos;&apos;', $xmlstring);
    
        header("Content-type: application/xml; charset=UTF-8");
        header("Content-Disposition: attachment;filename=". $this->sFileName .".xml");
        header("Cache-Control: max-age=0");

        exit($xmlstring);
    

This works great when there is no pre-processing required (permission checks, valid key checks, etc...)

However, if I have some server-side pre-processing to do prior to this portion, and it fails, I need a way to send a message back to the front end.

I know with what I have, it will not work. My only thought is that I will need to do the pre-processing in a separate AJAX call and then if it's successful, run the actual download but that isn't ideal. Some other solutions I've looked into include using fetch to get the data back and then potentially using a library to download the blob data but again, I'm not sure if I can tell whether or not it was actually a successful server request or not.

Thanks in advance.

jprice92 :

What I ended up doing is creating an iFrame for the download and the form will target it.

Now my download class will return an output array to the download endpoint where I can check if it passed the pre process, process, and post processing. If it did not succeed, I set the headers to back to text/html and inject a line of JS into the iFrame window where I can then target the parent window and call my error function with the message passed from the output data.

My PHP download endpoint:

if (!$aOutput['success']){
    header("Content-type: text/html; charset=UTF-8");
    header("Content-Disposition: inline");
    exit('<script type="text/javascript">if (window.parent.Footer) {window.parent.Footer._download.error(\''. $aOutput['data'] .'\') } else { window.parent.w360.error(\'' . $aOutput['data'] . '\', \'Download Failed\')}</script>');
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do you use PHP to send form data as a message?

PHP Form Submission without a client

How do I show message on 403 client side if the email fails to send?

How do you send a private message on_ready aka on a @client.event. Discord.py

Socket.io 1.4.5 How do you send a message to an individual client in a room?

How do you send a variable when including a php via shortcode?

How do you change a html text after form submission?

How do you pass a selection button into the params passed with a form submission?

PHP WebSocket – How send message to specified client?

How do you send a message using pyOSC?

how do you download an image in the form of bytes?

Unable to Block Form Submission When Validation Fails

jQuery - How to add "Thank you" message after embedded Mailchimp form email submission?

How to send email with form submission on Netlify?

How do I send message to specific client in node.js

How do you tell if the message is from send or sendSync in the main process?

How do you send message using NFC with Xamarin.Android?

How do you post a form with ajax in php?

How do you make a discord bot that send a message if another user send a message? discord.py

HTML/PHP Form Submission (what to do after)

How Do You Redirect To Varying URLS From A Form Submission Depending On The Radio Value Selected

How do you get my rails radio_button_tag to save the data on form submission?

How do I send form data from the client side to the server

How to send client side javascript date in form that calls a PHP file

Angularjs prevent form submission when input validation fails

Prevent form submission when validation fails (Angular Material)

Send Message when NServiceBus Recoverability fails

How use PHP function for action in form submission?

Send message to a client when an HTTP method not supported