将CSV文件直接下载到用户下载文件夹 [英] Download a CSV file straight to the users download folder

查看:294
本文介绍了将CSV文件直接下载到用户下载文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我想在我的网站上有选择让用户下载一个CSV文件。我已经使用了下面的代码:

 < input type =buttonvalue =Download as CSV filewindow.location。 href ='call_log.csv'/> 

这种方法可行,但当点击按钮时文件在我的浏览器的另一个选项卡中打开,我想要发生的是直接下载到用户的默认下载文件夹。



我之前发布了此问题,回复是包括标题,例如:

内容处理:附件;文件名=call_log.csv

页面是一个PHP文件,如果我包含标题页面不加载,但trys下载整个页面。
当然,我不能把头文件放在CSV文件中,任何人都可以帮我吗?



Thanks

解决方案

您的服务器需要下注以执行您的php脚本 - 您是对的;需要改变这一点。



你需要做的是发送正确的头到ser ver 来自您的php脚本。以下是 php.net 的示例:

 <?php 
//我们将输出PDF
头('Content-type:application / pdf');

//它将被称为downloaded.pdf
header('Content-Disposition:attachment; filename =downloaded.pdf'');

// PDF源文件位于original.pdf中
readfile('original.pdf');
?>

对于您的csv文件,正确的内容类型是 text / csv


Hi I want to have the option on my site for the user to download a CSV file. I have used the code below

 <input type="button" value="Download as CSV file" window.location.href='call_log.csv'  " />

This does work but when the button is clicked the file is opened in another tab on my browser, What I want to happen is a download straight to the users default download folder

I posted this question before and the response was to include headers ie

Content-Disposition: attachment; filename="call_log.csv"

The page is a php file and if I include headers the page does not load but trys to download the whole page . Surely I cant put headers in the CSV file , can anyone help me please ?

Thanks

解决方案

Your server needs to bet set to execute your php script - you're right; there's no need to change that.

What you need to do is send the correct header to the server from your php script. Here's an example from php.net:

<?php
// We'll be outputting a PDF
header('Content-type: application/pdf');

// It will be called downloaded.pdf
header('Content-Disposition: attachment; filename="downloaded.pdf"');

// The PDF source is in original.pdf
readfile('original.pdf');
?>

For your csv file, the correct content type is text/csv

这篇关于将CSV文件直接下载到用户下载文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆