我最近一直在 VS Code 中处理一些项目,突然开始在我的代码中收到 btoa 和 atob 现已弃用的通知。除了 VS Code 之外,我找不到任何相关资源。如果这是真的,还有什么替代方案呢?
我正在使用Spring MVC.我必须编写一个服务,它将从请求体中获取输入,将数据添加到pdf并将pdf文件返回给浏览器.使用itextpdf生成pdf文档.我怎么能用Spring MVC做到这一点.我试过用这个
@RequestMapping(value="/getpdf", method=RequestMethod.POST)
public Document getPDF(HttpServletRequest request , HttpServletResponse response,
@RequestBody String json) throws Exception {
response.setContentType("application/pdf");
response.setHeader("Content-Disposition", "attachment:filename=report.pdf");
OutputStream out = response.getOutputStream();
Document doc = PdfUtil.showHelp(emp);
return doc;
}
Run Code Online (Sandbox Code Playgroud)
showhelp生成pdf的函数.我暂时将一些随机数据放在pdf中.
public static Document showHelp(Employee emp) throws Exception {
Document document = new Document();
PdfWriter.getInstance(document, new FileOutputStream("C:/tmp/report.pdf"));
document.open();
document.add(new Paragraph("table"));
document.add(new Paragraph(new Date().toString()));
PdfPTable table=new PdfPTable(2);
PdfPCell cell = new PdfPCell (new Paragraph ("table"));
cell.setColspan (2);
cell.setHorizontalAlignment (Element.ALIGN_CENTER);
cell.setPadding (10.0f);
cell.setBackgroundColor (new BaseColor (140, 221, 8));
table.addCell(cell);
ArrayList<String[]> …Run Code Online (Sandbox Code Playgroud) 我想从AJAX响应中下载以字节形式出现的文件.
我试图通过以下方式这样做Bolb:
var blob=new Blob([resultByte], {type: "application/pdf"});
var link=document.createElement('a');
link.href=window.URL.createObjectURL(blob);
link.download="myFileName.pdf";
link.click();
Run Code Online (Sandbox Code Playgroud)
它实际上是下载pdf文件但文件本身已损坏.
我怎么能做到这一点?
我有一个byte[],我正在寻找最有效的base64编码方式.
问题是内置的.Net方法Convert.FromBase64CharArray需要一个char[]输入,并将我转换byte[]为char[]只是将它再次转换为base64编码的数组似乎非常愚蠢.
还有更直接的方法吗?
[[编辑:]]我会告诉我想要更好的实现 - 我有一个byte[],我需要返回一个新的base64编码byte[]
我正在下载带有axios的 zip 文件。为了进一步处理,我需要获取已下载的“原始”数据。据我所知,在 Javascript 中有两种类型:Blob 和 Arraybuffers。两者都可以responseType在请求选项中指定。
在下一步中,需要解压缩 zip 文件。我为此尝试了两个库:js-zip 和 adm-zip。两者都希望数据是一个 ArrayBuffer。到目前为止一切顺利,我可以将 blob 转换为缓冲区。在此转换之后,adm-zip 总是很高兴地提取 zip 文件。但是,js-zip 会抱怨文件损坏,除非 zip 已'arraybuffer'作为 axios下载responseType。js-zip 不适buffer用于从blob.
这让我很困惑。我想这两个ArrayBuffer和Blob本质上的底层内存只是意见。将某些内容下载为 blob 与缓冲区之间可能存在性能差异。但是结果数据应该是一样的吧?
好吧,我决定进行实验并发现:
如果指定responseType: 'blob',axios 会将 转换response.data为字符串。假设您对该字符串进行哈希处理并获得哈希码 A。然后将其转换为缓冲区。对于这种转换,您需要指定一种编码。根据编码的不同,您将获得各种新的哈希值,我们称它们为 B1、B2、B3,...当指定 'utf8' 作为编码时,我将返回原始哈希值 A。
所以我猜当下载数据为 a 时'blob',axios 隐式地将其转换为用 utf8 编码的字符串。这似乎非常合理。
现在您指定responseType: 'arraybuffer'. Axios 为您提供了一个缓冲区作为response.data. 对缓冲区进行哈希处理,您会得到一个哈希码 C。此代码与 A、B1、B2 中的任何代码都不对应,...
那么当下载数据为 时'arraybuffer',你得到完全不同的数据? …
好的,假设我将文档数据存储在某个地方,让我们任意选择这个pdf.
问题#1.我想要做的是对这个URL进行AJAX调用(因为我需要传递一些身份验证标头,它是跨域的).然后获取返回的数据,为它创建一个blob url,将一个iFrame附加到DOM,并将其src指向blob url.
目前我的代码如下所示:
$.ajax({
url:'http://www.grida.no/climate/ipcc_tar/wg1/pdf/tar-01.pdf'
}).done(function(data){
var file = new Blob([data], {type:'application/pdf'}),
url = URL.createObjectURL(file),
_iFrame = document.createElement('iframe');
_iFrame.setAttribute('src', url);
_iFrame.setAttribute('style', 'visibility:hidden;');
$('#someDiv').append(_iFrame);
});
Run Code Online (Sandbox Code Playgroud)
不幸的是,我在iFrame中遇到了"无法渲染PDF"的问题.
问题#2.我希望这会导致文件下载提示.鉴于PDF自然只会在iFrame中显示,因此不确定如何保证这一点.
我在使用EVOPdf将PDF 从WebAPI控制器渲染到AngularJS应用时遇到问题。
到目前为止,这是我的代码:
角度通话:
var url = 'api/form/build/' + id;
$http.get(url, null, { responseType: 'arraybuffer' })
.success(function (data) {
var file = new Blob([data], { type: 'application/pdf' });
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
window.navigator.msSaveOrOpenBlob(file);
}
else {
var objectUrl = URL.createObjectURL(file);
window.open(objectUrl);
}
});
Run Code Online (Sandbox Code Playgroud)
APIController方法:
var url = "http://localhost/index.html#/form/build/" + id;
#region PDF Document Setup
HtmlToPdfConverter htmlToPdfConverter = new HtmlToPdfConverter();
htmlToPdfConverter.LicenseKey = "4W9+bn19bn5ue2B+bn1/YH98YHd3d3c=";
//htmlToPdfConverter.HtmlViewerWidth = 1024; //default
htmlToPdfConverter.PdfDocumentOptions.PdfPageSize = PdfPageSize.A4;
htmlToPdfConverter.PdfDocumentOptions.PdfPageOrientation = PdfPageOrientation.Portrait;
htmlToPdfConverter.ConversionDelay …Run Code Online (Sandbox Code Playgroud) 我使用 puppeteer 在后端生成 pdf 并将其发送到前端,代码如下:
\nexports.createPdf = async (req, res) => {\n const { resumeContent } = req.body;\n console.log("datadatadatadatadatadatadatadatadatadata", resumeContent);\n const browser = await puppeteer.launch();\n const page = await browser.newPage();\n await page.setContent(resumeContent, { waitUntil: [\'domcontentloaded\', \'networkidle0\'] });\n const resumePdf = await page.pdf(\n // { path: \'./resume.pdf\' }\n );\n await browser.close();\n res.set({ \'Content-Type\': \'application/pdf\', \'Content-Length\': resumePdf.length });\n res.send(resumePdf);\n};\nRun Code Online (Sandbox Code Playgroud)\n如果我添加{ path: \'./resume.pdf\' },puppteer 会将 pdf 文件保存在后端服务器中,我已经检查过了,这正是我想要的。
现在我希望将它发送到前端而不是保存在后端,我的前端代码如下所示:
\nawait dispatch({\n type: \'resume/createResumePdf\',\n payload: {\n resumeContent\n }\n });\n …Run Code Online (Sandbox Code Playgroud)