php 点击按钮自动下载,IE下有问题,求解答!

直接看代码!

test.php

<?php
    include_once(‘index.php‘);
?>
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
    <a href="#" id="btn">点击下载</a>
    <script type="text/javascript">
        var str = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAABkCAYAAABw4pVUAAAGtUlEQVR4Xu2d4VLjMAyE6fs/NNd0CHFyUr9dxYECuhl+3MR2HK12tXJauL29vb3ff07/e3/flrndbvJ647x1UjY/GjveiO4b7dHZd3Z/uq8cjPvAJXINyEfEKLBfCghlX4by+hBZplEG0vXovsoceh7aN2U1sY3mZ8z+ZAg9QAOyj0ADMsTjzzHE0dKI+hkd10CO6xMzo7HOfIfZZBCohiiJst4jGxtKVgNydzuBU2xAFhs4BKbCsGZIEsQlMI68RdSOguv0LDOZH8nby0pWpvENyAbjl9YQKrpnr1c9PllVYtB437NrNSDLkURwvOPITANyCKLDjHWsI58vyxDnwZ0jCKfneLYHsp/Z3Fn3X9YntlVi+DA+y9rHG1QWU6g/KyANyBOEmiFs5ynBR6m8/PidqO00fgT+d9+LAq9cb0CCk4CsiSWDoAScxtzuWTXlBVV2IzoTqjCEHmqsidWeh5pYZQ+VMQ2IwZBKgN05DchPA4Soq16nLnemzGQ2nhq/SL2vPrw8MggZogbc6Q2ynkU9XleKqxPcNSjOHGdstH4GdAMypKgTZGfsJYBkkkNFi2Qimu+w7ey+Zt7LcXSpKyXbS7a1Adki0IAcssFpqdTamCUcSRZdTxlyvyAdLiqFlNjiyNM61pG8CiCK+3tW9Jdr6pGOEkP5tFdZrAGJP99M7nF3uLjWECfglIkztPRqNqnOhyz6uE/qWZQYf9peZTBRN5KZqpY2IHCE4NjLZsg+nSzJWot6Rf8dB6KsTwXckUpic6UQkwFw9pdJ4bT3IQ6DUssHX/RxHrgBGaJMgWtA4g9JPCx01Ic4NcDRR3I2VSDVniWTCWITyS2dZpA87pxaA7KFo5oQUwFRz7L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Cs56CsJjWgfdMelRqGp73qJhuQfaQctoWS5QQ0ygRnPmXa2eu/jiFELYf6TqZEhdS5FyWKA/S6lmPBSU2UpMXvh6h9hOOyyCVlgYuO553a1oDco9UM0c/KHlJ7//nvryOclSxiwNmspv2RPc2uk+TQc1WvY1GnByZdp401IDn0lzAk8+NqPXIynLKamrVq70AFmqQ621cDMiBCQXSkjtZqQBKD8SMYQjJAfp8ekiTNkYOqvKkHnUosaL/KGuuYy49Oznr/6GEU0+AYk89gCL/UhuqgE/zw2SLb6yxKzVoD4kRT+JCDupxC2wi8KJNprZkMUdaKGOTs0ZHHy9+HUL1pQPYp34AkdYOy+iqG/AOjmz5hjva3FgAAAABJRU5ErkJggg==";
        function $urlEncode(str){//直接post,浏览器会把+号转了的,这里有坑
            if(str && str.length){
                return escape(str).replace(/\+/g, ‘%2B‘).replace(/\"/g,‘%22‘).replace(/\‘/g, ‘%27‘).replace(/\//g,‘%2F‘);
            }
            return "";
        }
        document.getElementById(‘btn‘).onclick = function(){
            document.getElementById(‘btn‘).setAttribute(‘href‘,‘test.php?file=‘+ $urlEncode(str));
        }
    </script>
</body>
</html>

index.php

<?php
$base64_url = urldecode($_GET[‘file‘]);//先解码
if($base64_url){
    define(‘WWWROOT‘,  dirname(__FILE__).‘\\‘);
    $base64_url = preg_replace(‘/\s/‘, ‘+‘, $base64_url);//空格变+号
    $base64_body = substr(strstr($base64_url,‘,‘),1);
    $rand = rand(100, 999);
    $pics = date("YmdHis") . $rand . $type; //命名图片名称
    $filename = ‘./‘.$pics.‘.jpg‘;
    $name = $pics.‘.jpg‘;
    $img = base64_decode($base64_body);//解码
    $a = file_put_contents($filename, $img);//图片字节
    $path = WWWROOT.$name;//完整的文件保存路径
    $src = ‘http://127.0.0.1/download/‘.$name;//完整的图片url路径
    header("Cache-Control: no-cache");
    header("Pragma: no-cache");
    header("Expires: 0");
    header(‘Cache-Control: public, must-revalidate, max-age=0‘);
    header(‘Accept-Ranges: bytes‘);
    header("Content-Type: application/force-download");
    header("Content-Type: application/download");
    header("Content-Transfer-Encoding: binary");
    header("Content-type: application/octet-stream");
    header(‘Cache-Control: must-revalidate, post-check=0, pre-check=0‘); //不加的话,IE中会提示目标主机无法访问
    header(‘Pragma: public‘); //不加的话,IE中会提示目标主机无法访问
    header("Content-Length: ".filesize($path));
    header("Content-Disposition: attachment; filename=$name");
    $fp = fopen($src, ‘rb‘);
    fpassthru($fp);
    fclose($fp);
    if(file_exists($path)){//然后把图片删除掉
        unlink($path);
        exit;
    }
}
?>

亲测,chrome 和 火狐都没问题。但是在IE下有bug,求大牛指点!

时间: 2024-11-02 23:33:58

php 点击按钮自动下载,IE下有问题,求解答!的相关文章

新浪微博——点击按钮自动加关注代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-

JS实现点击按钮,下载文件

PS:本文说的,并非如何用js创建流.创建文件.实现下载功能. 而是说的:你已知一个下载文件的后端接口,前端如何请求该接口,实现点击按钮.下载文件到本地.(可以是zip啦.excel啦都是一样) 有两个方法:window.open()和通过form表单来提交. 在线例子:https://hamupp.github.io/gitblog/app/jsBasic/jsButtonDownloadFile/index.html 方法一:window.open("下载文件的后端接口"); *h

点击按钮自动刷新页面

<问题> <button type="button" style="height: 29px; float: left; width: 18%; border: none; border-left: 1px solid #d07878; border-radius: 0 4px 5px 0; padding-top: 3px" onclick="return search()"> 点击上述按钮,页面自动刷新 <原因&

2015-06-02 关于mvc表格点击按钮自动添加一行&lt;tr&gt;&lt;/tr&gt;

前台代码: @using (Html.BeginForm("ContactPerson", "User", FormMethod.Post, new { @class = "ContactPerson" })){ <div class="ope clearfix"> <input type="button" id="addNewPeople" class="b

tableVIew/scrollVIew,点击按钮滑倒最下/最上.

//scrollVIew1 //跳到最前 2 [_scrollView scrollRectToVisible:CGRectMake(0, 0, self.view.frame.size.width, 10) animated:YES]; 3 //跳到最后 4 [_scrollView scrollRectToVisible:CGRectMake(0, kHeight + 200 + _webView.frame.size.height, self.view.frame.size.width,

UIEditBox 控件的使用 点击输入框 自动切换 到下一个输入框 并上移 背景

Quick-3.5 local editAccount,editPwd local function editBoxEventHandler(strEventName,pSender) local edit = pSender:getName() if strEventName == "return" then if edit == "editAccount" then local _delay = function () if not tolua.isnull(e

jQuery点击按钮页面滚动条向下滚动

$("#btnAddAddress").click(function(){ $("body").animate({ 'scrollTop' : "+=400px" }, 300); }); $(document).scrollTop(400);

cdh hadoop 安装包自动下载脚本

cdh hadoop 安装包自动下载脚本 下面的shell脚本用于自动下载cdh5.2.1的hadoop rpm包. 测试通过. #!/bin/bash # # @file # cdh5_rhel6_x64_cdh5.2.1-downloads.sh # # @date # 2014-12-18 # # @author # cheungmine # # @version # 0.0.1pre # # download all RPMS from: # -- http://archive.clou

[python] 1、python鼠标点击、移动事件应用——写一个自动下载百度音乐的程序

1.问题描述: 最近百度总爱做一些破坏用户信任度的事——文库金币变券.网盘限速,吓得我赶紧想办法把存在百度云音乐中的歌曲下载到本地. http://yinyueyun.baidu.com/ 可问题是云音乐中并没有批量下载,而上面我总共存了700多首音乐! 因此:有必要写一个脚本自动下载这些音乐了!!! 2.解决问题 自动下载歌曲有两种方法: JS法 模拟鼠标点击法 由于考虑到JS法需要分析网页结构.寻找下载链接,工作量有点大,于是选择用模拟鼠标点击法! 在linux上我首先想到用python来做