doom910730 2011-04-12 11:41
浏览 163
已采纳

将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

  • 写回答

1条回答 默认 最新

  • dongmeng4742 2011-04-12 11:50
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看