直接看代码!
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/RxHrgBGaJMgWtA4g9JPCx01Ic4NcDRR3I2VSDVniWTCWITyS2dZpA87pxaA7KFo5oQUwFRz7LoEI6yb3nsiE1Uexy2OlaZgkisUPqPI/OU1uKS43en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nPmXa2eu/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