开发者

How do I display a message after a file has been downloaded?

开发者 https://www.devze.com 2023-03-02 04:48 出处:网络
I have this PHP code and it works fine. he开发者_JS百科ader(\'Content-Type: application/zip\'); header(\'Content-Disposition: attachment; filename=\"WTBak.zip\"\');

I have this PHP code and it works fine.

he开发者_JS百科ader('Content-Type: application/zip');
header('Content-Disposition: attachment; filename="WTBak.zip"');
readfile($ArchiveFileName);
echo $ArchiveFileName;
unlink($ArchiveFileName);

My issue is, how do I give out a message after the last line (unlink) has been executed?

Thanks!


Assumptions:

  1. The client user should receive a message, this is kind of message sent to the client

  2. The response is binary

Abstract:

  1. sending binary information to the client along with text response would be possible if it is mhtml format, used in mails and each browser has (some do nto have) the support for multipart response. Let us not chose this way

  2. sending binary information to respond one request (download file) and another response to another request (status of download) - this is a popular practice.

Solution:

  1. on server: persist the status of download

    // pseudocode: log_download_event(seessionid, status='started')

    header('Content-Type: application/zip');

    header('Content-Disposition: attachment; filename="WTBak.zip"');

    readfile($ArchiveFileName);

    echo $ArchiveFileName;

    unlink($ArchiveFileName);

    // pseudocode: log_download_event(seessionid, status='done')

  2. on server: implement a php that will respond with a status of download

    // pseudocode: $DownloadStatus=get_download_status(sessionid)

    echo '{staus:' + $DownloadStatus + '}'

  3. on client: on some event trigger the download

    window.open("http://nowhere.com/download.php?resuorce=archive-file.zip");

    window.theTrackInterval = window.setInterval(trackDownload, 1000);

    var trackInterval = function(){

    $.get('ajax/test.html', function(data) {

    id(data.status=='ready'){
    
        cleanInterval(window.theTrackInterval);
    
        alert('download is done');
    
    }
    

    });

    }

This solution will start sending the ajax requests to the server every one second asking "is download done" and when it will receive confirmation "yest it is done" client will stop tracking and alert a message

What is missed: the implementation of status persistence. i am not PHP guy - forgive me this gap


Look, if you give a message, it will be sent in the file and not shown to the user corrupting / changing the contents of the file. You cannot modify the headers as they've already been sent!

So, I feel, there is now way!

Cheers


I had a similar requirement from client. So I created a tool to show message after a file gets downloaded.

You can see at http://www.iamkumaran.com/xdownloader-a-flash-javascript-library/ and look for demo link.

0

精彩评论

暂无评论...
验证码 换一张
取 消