我已经为客户端和服务器端代码分配了一个示例代码,并进行了完美测试和工作。请根据您的需要进行修改。
服务器端:托管CLOUD9测试 - 因此,它需要通过process
对象由供应商提供的公网IP和端口。根据您的托管环境更改侦听器。
注:请阅读在线评论为更好地理解
var express = require('express');
var app = express();
var fs = require('fs');
var bodyParser = require('body-parser');
var formidable = require('formidable'),
form = new formidable.IncomingForm();
app.post("/", function(req, res, next) {
form.parse(req, function(err, fields, files) {
console.log("File received:\nName:"+files.pdf.name+"\ntype:"+files.pdf.type);
});
form.on('end', function() {
/* this.openedFiles[0].path -- object Contains the path to the file uploaded
------- Use NodeMailer to process this file or attach to the mail-----------------*/
console.log("PDF raw data:"+ fs.readFileSync(this.openedFiles[0].path, "utf8"));
res.status(200).send("thank you");
});
})
.listen(process.env.PORT, process.env.IP, function() {
console.log('Server app listening');
});
客户端:Fiddler
注:我没贴imgData
为SO有字符限制。请参阅fiddler链接以获取客户端代码。将请求URL更改为您的服务器。客户端使用Blob API,这是一个HTML5标准。所以请在兼容HTML5的浏览器上进行测试。
var imgData = [[**Copy the same image provided by JSpdf example. Check the fiddler for complete code**]]
var doc = new jsPDF();
doc.setFontSize(40);
doc.text(35, 25, "Octonyan loves jsPDF");
doc.addImage(imgData, 'JPEG', 15, 40, 180, 180);
var data = new Blob([doc.output()], {
type: 'application/pdf'
});
var formData = new FormData();
formData.append("pdf", data, "myfile.pdf");
var request = new XMLHttpRequest();
request.open("POST", "https://saltjs-nirus.c9.io"); // Change to your server
request.send(formData);
参考文献:
您可以使用[FormData](https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData)API –
@rahilwazir通过AngularJS发送文件但是文件是否在相同的格式..我的意思是在这种情况下发送到服务器后的PDF? – Amarnath
是相同的格式 –