duanjurong1347 2011-02-15 04:05
浏览 41
已采纳

告诉浏览器在Codeigniter中打开或下载文件

In Codeigniter, we can use the force_download($file_name,$data) function from the download helper to force the browser to download a file.

But this is not what I need, I would like Codeigniter to respond a http header with the MIME type that the file corresponds to, so that the browser may or may not download the file, depending on whether there is an application registered for that MIME type on the client machine (if there is one, that application will open that file).

Is there a way of doing this with Codeigniter?

Many thanks to you all.

  • 写回答

1条回答 默认 最新

  • dongsiju1941 2011-02-15 20:26
    关注

    No, you cannot do that with Codeigniter.

    Generally speaking, with server-side technologies like PHP, you can only send content to the client, not directly interact with the client. It is up to the client to do what they wish with whatever piece of content you send them. Even with client side tools like javascript, (thankfully) you can only have limited interaction with the client machine. What you are wanting to do is allow a website to (automatically) upon visiting, download a piece of software, and execute it on the client machine...

    that's called malware... (brought to you by the makers of ActiveX)

    Above was in direct response to

    "...so that the browser may or may not download the file, depending on whether there is an application registered for that MIME type on the client machine (if there is one, that application will open that file).

    UPDATE

    You can set the mime type through plain php header manipulation. The following example is taken directly from the php header() documentation as the first example.

    <?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');
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?