Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript/jquery下载图像_Javascript_Jquery - Fatal编程技术网

Javascript/jquery下载图像

Javascript/jquery下载图像,javascript,jquery,Javascript,Jquery,我想知道如何处理从标头响应下载的图像: HTTP/1.1 200 OK Date: Sun, 14 Jun 2020 23:23:29 GMT Server: Apache/2.4.39 (Win64) OpenSSL/1.0.2s PHP/7.1.30 X-Powered-By: PHP/7.1.30 Content-Disposition: attachment; filename="flyer.png" Cache-Control: no-cache, private Set-Cookie

我想知道如何处理从标头响应下载的图像:

HTTP/1.1 200 OK
Date: Sun, 14 Jun 2020 23:23:29 GMT
Server: Apache/2.4.39 (Win64) OpenSSL/1.0.2s PHP/7.1.30
X-Powered-By: PHP/7.1.30
Content-Disposition: attachment; filename="flyer.png"
Cache-Control: no-cache, private
Set-Cookie: XSRF-TOKEN=eyJpdiI6IkhTNWFcL29QQU11UkZOeWZka1ZZdlBBPT0iLCJ2YWx1ZSI6Inhwbm5WdEdCNnVqTlk1VVk5N2JjMHFsM2lmT3g1UmVhbHY5N3VtRjFmalI4VmM1STN3VUE5YWVPRHFZWVJab2dqZGFERnR4cGxmNktwdXNIWExuY2pnPT0iLCJtYWMiOiI4N2UxM2Y5MGM0MWJiNGVmNzJiMjdlYzc2NjQwODhjN2Q0OWM2ODM5MjA0MGRiYjQxZmY2ZDRiODViZDE5M2M1In0%3D; expires=Mon, 15-Jun-2020 01:23:31 GMT; Max-Age=7200; path=/
Set-Cookie: laravel_session=1IVT1owUoiIxjWKmDBF5WtJgHQCt0pDUZ39IGzeb; expires=Mon, 15-Jun-2020 01:23:31 GMT; Max-Age=7200; path=/; httponly
Keep-Alive: timeout=5, max=100
Connection: Keep-Alive
Transfer-Encoding: chunked
Content-Type: image/jpeg
下面是我的脚本的草图:

$.get("{!!route('gerarFlyerImage')!!}",dados).done(function(data){

    console.log(data);
    saveData(data, 'filename.png');    
})


function saveData(blob, fileName) // does the same as FileSaver.js
{
    var a = document.createElement("a");
    document.body.appendChild(a);
    a.style = "display: none";

    var url = window.URL.createObjectURL(blob);
    a.href = url;
    a.download = fileName;
    a.click();
    window.URL.revokeObjectURL(url);
}

但是saveData()不管用吗?

试试这个-从你的url下载图像

演示:

JS脚本

function downloadFile(fileName) {
    $.ajax({
        url: '{!!route('gerarFlyerImage')!!}',
        method: 'GET',
        data: dados,
        xhrFields: {
            responseType: 'blob'
        },
        success: function (data) {
            var a = document.createElement('a');
            var url = window.URL.createObjectURL(data);
            a.href = url;
            a.download = fileName;
            document.body.append(a);
            a.click();
            a.remove();
            window.URL.revokeObjectURL(url);
        }
    });
}
希望这有帮助