Visualizzazione dei risultati da 1 a 2 su 2
  1. #1

    Download di un file tramite jquery ajax

    ciao!

    avrei la necessità di eseguire il download di un file tramite ajax.
    da un bottone avvio questa funzione:
    codice:
    function raber() {
        var coll = $('#rab_coll').val();
        if (!coll) {
            alert('Choose a collection!');
        } else {
            $.ajax({
                type: "POST",
                dataType: 'binary',
                url: "anagrafiche_raber.php",
                data: {coll: coll.toUpperCase()},
                beforeSend: function (xhr) {
                    $('#modal_wait').modal('show');
                }
            }).done(function (res) {
                $('#modal_wait').modal('hide');
            }).fail(function (err) {
                $('#modal_wait').modal('hide');
                alert("ERRORE raber: " + err);
                console.log(err)
            });
        }
    }
    lato server:
    Codice PHP:
        header("Cache-Control: public");
        
    header("Content-Description: File Transfer");
        
    header("Content-Disposition: attachment; filename=$coll.dat");
        
    header("Content-Type: application/octet-stream");
        
    header("Content-Transfer-Encoding: binary");
        
    readfile($outFileExt); 
    però ottengo un errore quando provo ad eseguire la funzione (ma il file sul server viene cmq creato):
    codice:
    Object { readyState: 4, getResponseHeader: getResponseHeader(), getAllResponseHeaders: getAllResponseHeaders(), setRequestHeader: setRequestHeader(), overrideMimeType: overrideMimeType(), statusCode: statusCode(), abort: abort(), state: state(), always: always(), then: then(), altri 11… }
    qualche consiglio?

  2. #2
    ok, ho risolto così:
    codice:
    function raber() {
        var coll = $('#rab_coll').val();
        if (!coll) {
            alert('Choose a collection!');
        } else {
            $.ajax({
                type: "POST",
                url: '../' + urlService + "anagrafiche_raber.php",
                headers: {
                    Accept: 'application/octet-stream',
                },
                data: {coll: coll.toUpperCase()},
                beforeSend: function (xhr) {
                    $('#modal_wait').modal('show');
                }
            }).done(function (res) {
                const a = document.createElement('a');
                a.style = 'display: none';
                document.body.appendChild(a);
                const blob = new Blob([res], {type: 'octet/stream'});
                const url = URL.createObjectURL(blob);
                a.href = url;
                a.download = coll + '.dat';
                a.click();
                URL.revokeObjectURL(url);
                $('#modal_wait').modal('hide');
            }).fail(function (err) {
                $('#modal_wait').modal('hide');
                alert("ERRORE raber: " + err);
                console.log(err)
            });
        }
    }
    e lato server:
    Codice PHP:
        header("Cache-Control: public");
        
    header("Content-Description: File Transfer");
        
    header("Content-Disposition: attachment; filename=$coll.dat");
        
    header("Content-Type: application/octet-stream");
        
    header("Content-Transfer-Encoding: binary");
        
    header('Cache-Control: must-revalidate');
        
    header('Content-Length: ' filesize($outFileExt));
        
    readfile($outFileExt); 

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.