[英]forced file to download instead showing in the browser
I just want to download file when button gets clicked. 我只是想在点击按钮时下载文件。 I am using top.location.href
in jQuery to start downloading but this code work fine on localhost, but when I try this on server then instead of start downloading it display the file as text in the browser. 我在jQuery中使用top.location.href
开始下载,但是这个代码在localhost上运行正常,但是当我在服务器上尝试这个时,然后开始下载它在浏览器中将文件显示为文本。
This is what I am doing : 这就是我在做的事情:
$('#download_button').click(function(){
var filepath = $(this).attr("data-src");
// filepath = library/files/filename.git
top.location.href = filepath; // open download dialog box
});
The above code start downloading my custom file that contain 'xml' data by opening a dialog box on localhost but this code not work on server. 上面的代码通过打开localhost上的对话框开始下载包含“xml”数据的自定义文件,但此代码在服务器上不起作用。
Is anything I am doing wrong? 我做错了什么?
I tried to find out the problem but everyone says add these line of code in your hyaccess file 我试图找出问题,但每个人都说在你的hyaccess文件中添加这些代码
<FilesMatch "\.(?i:git)$">
ForceType application/octet-stream
Header set Content-Disposition attachment
</FilesMatch>
But I never found htaccess file on my server. 但我从来没有在我的服务器上找到htaccess文件。 Can I create a new file with in my directory and add this code into it ? 我可以在我的目录中创建一个新文件并将其添加到其中吗?
您可以使用HTML5下载属性
<a href="/something.git" download="name-of-file">Click to download</a>
try this code 试试这段代码
$('#download_button').click(function(){
var filepath = $(this).attr("data-src");
// filepath = library/files/filename.git
var url = 'library/files/download.php';
var inputfied = '<input type="text" name="filepath" value="' + filepath + '" />';
var form = $('<form action="' + url + '" method="POST" >' +inputfied +'</form>');
$('body').append(form);
$(form).submit();
});
and use this page download.php
并使用此页面download.php
<?php
$file=$_POST['filepath'] ;//file location;
header('Content-Type: application/octet-stream');
header('Content-Disposition: attachment; filename="'.basename($file).'"');
header('Content-Length: ' . filesize($file));
readfile($file);
?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.