Javascript和PHP发送下载请求
我有一个WebApp,它必须创建和下载一个文件 文件内容和标题是通过javascript设置的,但我不知道如何让它下载: 这是JS: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
$('#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(全局文件内容)代码>