最后,经过数周的自学后,我设法让PhantomJS(在NodeJS中运行)处理一个文件,其中包含属性,输出为PNG或PDF,这非常有效。
但是,过去几天我一直被困住,并希望有人能指点我。
当PhantomJS创建PDF并且我的子进程已完成时,如何显示一个对话框,询问用户是否要下载该文件?
我使用了以下代码:
childProcess.execFile(binPath, childArgs,
function(err, stdout, stderr) {
console.log('Child Processing complete!');
download('myservername','test.png',function(){
console.log('done')
});
}
);
使用"下载"功能如下:
var download = function(uri, filename, callback){
request.head(uri, function(err, res, body){
console.log('content-type:', res.headers['content-type']);
console.log('content-length:', res.headers['content-length']);
var r = request(uri).pipe(fs.createWriteStream(filename));
r.on('close', callback);
});
};
但我似乎无法在屏幕上看到提示我下载" test.png"
我有什么明显的遗失吗?
*编辑*
此处的实际代码:
这是调用的页面:
var util = require("util"),
path = require('path'),
fs = require('fs'),
request = require('request'),
url = require('url'),
http = require('http');
var phantomjs = require('phantomjs'),
path = require('path'),
childProcess = require('child_process');
childProcess2 = require('child_process');
var express = require('express');
var app = express();
var download = function(uri, filename, callback){
request.head(uri, function(err, res, body){
console.log('content-type:', res.headers['content-type']);
console.log('content-length:', res.headers['content-length']);
var r = request(uri).pipe(fs.createWriteStream(filename));
r.on('close', callback);
});
};
var binPath = phantomjs.path;
http.createServer(function(request,response){
console.log("Webpage was called");
var childArgs = [path.join(__dirname, 'tst2.js'),''];
childProcess.execFile(binPath, childArgs,
function(err, stdout, stderr) {
console.log('Child Processing complete!');
download('[here is the url of my server with filename.png','test2.png',function(){
console.log('done')
});
});
response.writeHeader(200, {"Content-Type": "text/plain"});
response.write("Processing Server");
response.end();
}).listen(8080);
和phantomJS页面在这里:
"use strict";
var page = require('webpage').create();
page.viewportSize = { width: 1663, height : 768};
page.paperSize = {
width: '11in',
height: '8.5in'
}
page.orientation='landscape'
page.onResourceRequested = function(requestData, request) {
console.log('::loading', requestData['url']);
};
page.onLoadFinished = function() {
console.log('::rendering');
page.render('test.png');
phantom.exit();
};
var content='';
content += '<html><head>'
content +='</head><body>';
content+='<div class="col-md-12 col-lg-12 results">'
content+='<div class="col-md-8 detailsPanel">'
content+='</div>'
content +='</body></html>';
page.content=content;
就像我说的,它们都工作得很好,我调用的网页在端口8080上运行,我可以看到console.logs出现在Windows Server的命令框中,PNG文件完美呈现,但之后它只是挂在那里我没有下载提示。