通过FTP

时间:2018-04-30 09:00:18

标签: node.js ftp polymer polymer-2.x

我在远程服务器上有PDF。我有节点的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();
});

在close事件中,我将文件放在String中,但我没有成功将其发送到浏览器。我尝试过这样的事情:

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

OR

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> 

任何解决方案?

由于

1 个答案:

答案 0 :(得分:1)

我有一个名为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>