【问题标题】:How to change filename and extension while downloading a text file from data uri [duplicate]从数据uri下载文本文件时如何更改文件名和扩展名[重复]
【发布时间】:2015-01-01 15:10:52
【问题描述】:

当我下载数据 uri(这适用于 chrome/FF)时,使用

url1 = "data:attachment/plain;base64,encodeuri(<base64data-xyz>)"

这里使用附件 mime 类型强制它下载,使用任何其他 mime 类型(如(text/html)将在新选项卡中打开文件的内容,因为我想下载,我使用了 attachment/plain-xyz [MIME 类型].

我尝试了下载属性,但每次下载的文件名都是“下载”,并且没有扩展名,所以它在我的下载文件夹中附加为“.FILE”,而我需要使用“变量”来存储它.txt"

请指导我如何将“download.FILE”更改为某个“variable.txt”,我已经尝试了数据、下载、data-downloadurl、contentdisposition 的所有属性,甚至尝试设置 pom 属性等。

请帮助将下载的文件内容转换为具有某些文件名和.txt作为扩展名的文件

【问题讨论】:

    标签: javascript angularjs


    【解决方案1】:
    // Sanitizing the filename:
    $filename = preg_replace('/[^a-z0-9\-\_\.]/i','',$_POST['filename']);
    
    // Outputting headers:
    header("Cache-Control: ");
    header("Content-type: text/plain");
    header('Content-Disposition: attachment; filename="'.$filename.'"');
    

    或thisk链接可能会有所帮助 [http://www.jtricks.com/bits/content_disposition.html][1]

    【讨论】:

    • 问题是关于 JS,而不是 PHP。
    【解决方案2】:

    FileSaver.js 提供了处理这些问题的简单方法

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-15
      • 2020-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-17
      • 1970-01-01
      • 2017-08-29
      相关资源
      最近更新 更多