- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在将 SVG 导出到 PNG 图像。现在我想将此 PNG 图像和 CSV 文件下载到 ZIP 文件夹中。我使用 JSZIP 来将两个文件压缩到 ZIP 文件夹中。现在当我单击在下载按钮上,ZIP 下载。下载的 ZIP 包含所需的 CSV 文件,但包含空白图像。如何将所需的图像下载到所需的 ZIP 文件夹中?
我当前的代码是:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<link href="../build/nv.d3.css" rel="stylesheet" type="text/css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/3.5.2/d3.min.js" charset="utf-8"></script>
<script src="../build/nv.d3.js"></script>
<script type="text/javascript" src="FileSaver.js"></script>
<script type="text/javascript" src="jszip.js"></script>
<style>
text {
font: 12px sans-serif;
}
svg {
display: block;
}
html, body, #chart1, svg {
margin: 0px;
padding: 0px;
height: 100%;
width: 100%;
}
.dashed {
stroke-dasharray: 5,5;
}
</style>
<script type="text/javascript">
function download()
{
img = new Image(),
serializer = new XMLSerializer(),
svgStr = serializer.serializeToString(document.getElementById('svg'));
img.src = 'data:image/svg+xml;base64,'+window.btoa(svgStr);
// You could also use the actual string without base64 encoding it:
//img.src = "data:image/svg+xml;utf8," + svgStr;
var canvas = document.createElement("canvas");
var w=3000;
var h=3000;
canvas.width = w;
canvas.height = h;
canvas.getContext("2d").drawImage(img,0,0,w,h);
var imgURL = canvas.toDataURL("image/png");
var CSV="ab";
var zip = new JSZip();
zip.file("abc.csv", CSV);
zip.file("img.png", imgURL);
content = zip.generate();
location.href="data:application/zip;base64," + content;
}
</script>
</head>
<body >
<div id="chart1" width="100%" height='100%'></div>
<button onclick="download()">Download</button>
<script>
var data = [{"color":"#a215af","key":"products","values":[
{"y":0,"x":0},
{"y":0,"x":1},
{"y":1,"x":2},
{"y":6,"x":3},
{"y":2,"x":4},
{"y":0,"x":5},
{"y":13,"x":6}]}]
nv.addGraph(function() {
chart = nv.models.lineChart()
.options({
transitionDuration: 300,
useInteractiveGuideline: true
})
;;
var days = ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"];
chart.xAxis
.rotateLabels(-45)
.tickValues([0, 1, 2, 3, 4, 5, 6])
.tickFormat(function(d){
return days[d]
});
chart.yAxis
.axisLabel('Voltage (v)')
.tickFormat(function(d) {
if (d == null) {
return 'N/A';
}
return d3.format(',.2f')(d);
});
d3.select('#chart1').append('svg')
.datum(data)
.attr("id","svg")
.attr("height","1000")
.attr("width","1000")
.call(chart);
nv.utils.windowResize(chart.update);
return chart;
});
</script>
</body>
</html>
最佳答案
在您的例子中,您告诉 JSZip 图像的内容是字符串 data:image/png;base64,iVBORw0K...
。最简单的解决方案是删除 data:image/png;base64,
部分并使用 base64: true
:
zip.file("img.png", imgURL.slice("data:image/png;base64,".length), {base64:true});
但是创建/解析 base64 字符串效率不高(我还通过您的代码获得了 3000x3000 灰度图像,但这无关)。
您还可以使用Blob
来避免base64操作(并使用JSZip v3来支持它):canvas.toBlob
以blob形式获取png文件,URL.createObjectURL
生成中间图像的url。
此解决方案的示例(我无法获得完美的图像,但您的问题(将图像放入 zip 文件中)已修复):
var canvas = document.createElement("canvas");
canvas.width = 1000;
canvas.height = 1000;
var ctx = canvas.getContext("2d");
var serializer = new XMLSerializer(),
svgString = serializer.serializeToString(document.getElementById('svg'));
var svg = new Blob([svgString], {type: "image/svg+xml;charset=utf-8"});
var url = URL.createObjectURL(svg); // <-- create a blob url for the svg image
var img = new Image();
img.onload = function() {
// draw the svg to a canvas
ctx.drawImage(img, 0, 0);
URL.revokeObjectURL(url);
canvas.toBlob(function (blob) { // <-- convert the canvas to a png (in a blob)
var zip = new JSZip();
zip.file("abc.csv", CSV);
zip.file("img.png", blob); // <-- JSZip v3 accepts blob
content = zip.generateAsync({type:"blob"}).then(function (blob) {
saveAs(blob, "result.zip"); // <-- trigger the download
}, function (e) {
console.error(e)
});
}, "image/png");
};
img.src = url; // <-- load the blob url
关于javascript - 如何将从 SVG 元素和 csv 文件生成的 PNG 图像导出到可下载的 ZIP 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37451210/
我有这个命令: 7z e -oD:\Data\ODS_Source\* D:\Data\DATA_DROP\Source.zip 这导致 D:\Data\ODS_Source\Source\. 我需要
我正在尝试让 Ionic zip 将 zip 文件内的文件夹提取到指定的根目录中。我的问题是里面的zip文件是“zipfile.zip\some_folder\”。我想将“some_folder”中的
我试图让 Ionic zip 将 zip 文件中的文件夹提取到指定的根目录中。我的问题是里面的 zip 文件是“zipfile.zip\some_folder\”。我想将“some_folder”中的
题目 监听服务器端口,得到题目如下: 源码解析 主函数 主函数中是题目界面的逻辑,对应于用户的选择做出相应的操作,其中需要注意的是选项2,解压操作需要获得root权
我有许多需要分发给用户的zip文件,其中约有130个。每个zip文件都包含许多相似的文本,html,xml和jpg文件。压缩文件总计146兆字节;解压缩后,其内容总计551mb。 我想将所有这些文件以
我正在使用 javascript zip.js图书馆。我到处搜索,但找不到将多个文件添加到 zip 的示例。 这是我的代码,但它生成了一个“损坏的”zip。 var len = results.row
在 C# 中,我使用的是 DotNetZip我有一个名为“innerZip.zip”的 zip,其中包含一些数据,和另一个名为“outerZip.zip”的 zip,其中包含 innerZip。我为什
当我使用 library(xlsx) 中的 write.xlsx 时,控制台中会出现以下内容: Note: zip::zip() is deprecated, please use zip::zipr
如果我因为问“非编程”问题而被拒绝,我不会太惊讶,但也许有人知道...... 我正在使用 WinXP 的内置“发送到压缩(zipped)文件夹”功能压缩我的 subversion 沙箱的内容,并惊讶地
我在 Elixir 中有一个二进制字符串,它由压缩字节组成,我想放气并从中提取“真实数据”: iex(93)> data > 我不确定如何解压缩这些数据。到目前为止,我已经: 浏览了 Official
有没有一种方法可以创建一个 zip 文件并强制它在命令行中包含数据描述符部分? 最佳答案 在 Github ( https://github.com/adamhathcock/sharpcompres
我已经有 PBDT.csj and RDK.csj使用此 ( https://www.blackberry.com/SignedKeys/codesigning.html ) 链接进行代码签名处理后的
我研究了几天,发现我们可以将一个包含一些内容的文件添加到 zip 文件中,然后再次压缩它。然后注释将被添加到 zip 文件中,但我不知道该文件到底是什么,所以任何人都知道向 zip(压缩)文件添加注释
我想知道如何找到 zip 文件的压缩级别。 7z 和 winzip 制作的 Zip 文件具有不同的级别评级,因此我想将其中的一些映射到其他工具中的相应级别。 store level 或 level 0
到目前为止,对于Zip文件的Mime类型,我已经看到: 应用程序/八位字节流 multipart / x-zip 应用程序/ zip 应用程序/ zip压缩的 应用程序/ x-zip压缩的 我想我的问
我已经在 google 上搜索、在 wiki 上搜索并阅读了 ZIP 的 RFC,但找不到有关 ZIP 中使用的确切算法的任何信息。 我找到了有关 ZIP == TAR + GZIP 的信息 但是,我
我有这些自解压 zip 文件,我正试图在 2008/7 机器上远程解压这些文件。但它们是以 .exe 的方式出现的,它需要用户双击并选择提取位置。 在 WinZip 支持网站上,他们说要使用/auto
这是我在这里的第一个问题,请耐心等待。 我的目标是在 C# 中创建一个基本的 .zip 存档。我已经尝试使用 .NET 的内置 GZipStream 类并设法实现了这一点,但是我遇到了一个问题,我无法
能否为压缩文件中的压缩文件创建 java.nio.file.FileSystem? 如果是这样,URI 是什么样的? 如果没有,我想我将不得不退回到使用 ZipInputStream。 我正在尝试递归
我想在 here 安装 scala我很关心下载哪一个:zip 还是 tgz。它们之间有什么区别,用例是什么? 最佳答案 它们是不同的archive formats .使用它们是因为它可以节省带宽并且因
我是一名优秀的程序员,十分优秀!