Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
File 强制下载';数据:文本/纯文本';统一资源定位地址_File_Text_Download_Content Disposition_Data Url - Fatal编程技术网

File 强制下载';数据:文本/纯文本';统一资源定位地址

File 强制下载';数据:文本/纯文本';统一资源定位地址,file,text,download,content-disposition,data-url,File,Text,Download,Content Disposition,Data Url,我想知道是否有可能强制浏览器(至少是Chrome)下载data:text/plainURL Chrome会下载二进制URL(例如,数据:application/zip;base64,),但不会下载可在浏览器内查看的文件(如文本文件) 到目前为止,我已经尝试过但没有运气的是: data:text/plain;content-disposition=attachment;filename=test.txt;... 但我似乎无法添加这样的标题 有没有办法让Chrome下载一个data:text/pl

我想知道是否有可能强制浏览器(至少是Chrome)下载
data:text/plain
URL

Chrome会下载二进制URL(例如,
数据:application/zip;base64,
),但不会下载可在浏览器内查看的文件(如文本文件)

到目前为止,我已经尝试过但没有运气的是:

data:text/plain;content-disposition=attachment;filename=test.txt;...
但我似乎无法添加这样的标题


有没有办法让Chrome下载一个
data:text/plain,…
URL

我所做的是将数据发送到服务器,服务器使用以下HTTP头将数据发送回:

Content-disposition: attachment;filename=test.txt

我不喜欢这个,但它工作得相当好。

到目前为止,已经可以在Chrome中使用
。使用
dispatchEvent
,您可以随时下载任何字符串作为文件(即使使用自定义文件名)。下面是使用它的实用程序函数:

var downloadFile = function(filename, content) {
  var blob = new Blob([content]);
  var evt = document.createEvent("HTMLEvents");
  evt.initEvent("click");
  $("<a>", {
    download: filename,
    href: webkitURL.createObjectURL(blob)
  }).get(0).dispatchEvent(evt);
};
它使用jQuery和
webkit
前缀,但两者都可以避免。

尝试以下方法:


它使用HTML5属性
download=“filename.ext”
。(不需要JS:)

更多关于:

可在以下位置查看浏览器支持:

(截至目前,2013年,没有IE或Safari支持)

我认为,您可以对不支持浏览器进行回退:使用JS将
href=“…”
的值更改为服务器脚本的URL(该脚本将返回带有适当HTTP头的文件内容
内容配置:附件;filename=filename.txt
)。

这就像地狱一样有效

<div class="tags-style-one dragme" draggable="true" data-transfer="33343">some value is 33343</div>

    <script type="text/javascript">
    (function ($) {
        $(document).ready(function () {
        $('.dragme').on("dragstart",function(evt) {
            evt.originalEvent
                    .dataTransfer
                    .setData(
                            "text/plain",
                            $(this).data('transfer').toString()
                    );
        });
    })(jQuery);
</script>
某些值是33343
(函数($){
$(文档).ready(函数(){
$('.dragme')。on(“dragstart”,函数(evt){
原始事件
.数据传输
.setData(
“文本/纯文本”,
$(this).data('transfer').toString()
);
});
})(jQuery);

这里是一个纯Javascript解决方案,用于创建文本blob并作为文本文件下载

var fileContent = 'This is sample text file';
var fileName = 'sampleFile.txt';

const blob = new Blob([fileContent], { type: 'text/plain' });
const a = document.createElement('a');
a.setAttribute('download', fileName);
a.setAttribute('href', window.URL.createObjectURL(blob));
a.click();

看起来真的很棒,而且这个功能在Chrome上的广告中也可以使用。(我已经在使用jQuery了)是否可以修改此函数,使其在Firefox和/或Safari中也能工作?我对
initEvent
Blob
createObjectURL
了解得不够。谢谢。@Harold Safari不支持
download
属性。这太棒了,一些新浏览器不支持导航到data url现在在2017年我尝试了其他解决方案,但它在Microsoft Edge上始终不起作用。但这个简单的解决方案似乎在Chrome、Mozilla和Microsoft Edge上运行良好。谢谢!
var fileContent = 'This is sample text file';
var fileName = 'sampleFile.txt';

const blob = new Blob([fileContent], { type: 'text/plain' });
const a = document.createElement('a');
a.setAttribute('download', fileName);
a.setAttribute('href', window.URL.createObjectURL(blob));
a.click();