Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/440.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和PHP发送下载请求_Javascript_Php - Fatal编程技术网

Javascript和PHP发送下载请求

Javascript和PHP发送下载请求,javascript,php,Javascript,Php,我有一个WebApp,它必须创建和下载一个文件 文件内容和标题是通过javascript设置的,但我不知道如何让它下载: 这是JS: $('#GPL_export').click(function(){ window.location = base_url()+'service/createGPLFile/'+global_filename_download+'/'+global_file_content; }); 这是PHP: public function create

我有一个WebApp,它必须创建和下载一个文件

文件内容和标题是通过javascript设置的,但我不知道如何让它下载:

这是JS:

$('#GPL_export').click(function(){
      window.location = base_url()+'service/createGPLFile/'+global_filename_download+'/'+global_file_content;
    });
这是PHP:

public function createGPLFile($filename, $content){

    $f = fopen($filename, 'w');
    fwrite($f, $content);
    fclose($f);

    header("Cache-Control: public");
    header("Content-Description: File Transfer");
    header("Content-Length: ". filesize("$filename").";");
    header("Content-Disposition: attachment; filename=$filename");
    header("Content-Type: application/octet-stream; "); 
    header("Content-Transfer-Encoding: binary");

    readfile($filename);
}

问题是,通过URL发送文件内容(我有一个URI函数,它将URI字符串作为函数参数)不起作用,因为我的文件内容包含不同的符号,它们会转换为URL字符。

您是否尝试过使用
rawurldecode($content)
?是否尝试过使用urldecode()在读取文件内容之前对其进行检查?可能是
window.location=base_url()+'service/createGPLFile/'+encodeURIComponent(全局文件名下载)+'/'+encodeURIComponent(全局文件内容)