gpt4 book ai didi

node.js - 通过 FTP 从 Node API 下载 PDF

转载 作者:太空宇宙 更新时间:2023-11-03 22:08:12 28 4
gpt4 key购买 nike

我在远程服务器上有 PDF。我有带有 Node 的 API,我想从我的网站下载 PDF。

我正在使用 jsftp 上传和阅读 PDF。它工作正常:

let str = '';
FTP.get('path/to/my/file', (err, socket) => {
socket.on("data", d => {
str += d.toString();
});
socket.on("close", err => {
if (err) {
console.error("There was an error retrieving the file.", err);
}
// HERE I HAVE THE FILE IN STRING
});
socket.resume();
});

在关闭事件中,我在字符串中保存了该文件,但我没有成功将其发送到浏览器。我尝试过类似的事情:

let s = new Readable();
s.push(str);
s.push(null);
s.pipe(res);

或者

res.end(str);

但浏览器中没有任何反应

我正在使用 Polymer 来处理我的 ajax 请求

<iron-ajax
id="openPdf"
content-type="application/json"
method="POST"
url="/api/karaweb/pdf"
on-response="handleOpenPdfResponse"
on-error="errorMessage"></webservice-request>

有什么解决办法吗?

谢谢

最佳答案

我有一个名为 PdfDownloaderMixin 的 mixin;这是完整的代码:

<link rel="import" href="../../bower_components/polymer/polymer-element.html">

<dom-template id="pdf-downloader-mixin">
<script>
/* @polymerMixin */
const PdfDownloaderMixin = (superClass) => class extends superClass {
constructor() {
super();
}

downloadPdf(blobData) {
let fileObjectUrl = window.URL.createObjectURL(blobData);
window.open(fileObjectUrl);
}
}
</script>
</dom-template>

然后你在你的元素中使用这样的:

<link rel="import" href="../../bower_components/polymer/polymer-element.html">
<link rel="import" href="../../bower_components/iron-ajax/iron-ajax.html" />
<link rel="import" href="pdf-downloader-mixin.html" />

<dom-module id="attachment-handler">
<template>
<iron-ajax id="getAttachmentAjax"
url="[[rootPath]]api/session/getattachment"
debounce-duration="300"
handle-as="blob"></iron-ajax>
</template>
<script>
class AttachmentHandler extends PdfDownloaderMixin(Polymer.Element) {
static get is() { return 'attachment-handler'; }

getAttachment() {
this.$.getAttachmentAjax.generateRequest().completes.then((req) => {
req.succeeded && this.downloadPdf(req.response);
});
}
}

customElements.define(AttachmentHandler.is, AttachmentHandler);
</script>
</dom-module>

关于node.js - 通过 FTP 从 Node API 下载 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50097189/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com