NodeJs-将pdf上传到远程服务器

时间:2018-09-28 15:27:12

标签: node.js express

我是nodejs的新手,正在尝试将pdf文件发布到远程服务器,但一直收到 HTTP错误404。找不到请求的资源。

我将服务器代码发布到下面:-

const express = require('express');
var http = require('http');

const app = express();
var request = require('request');

var fs = require('fs');

const dirName = 'C:\\Users\\abhinav.mehrotra\\Desktop\\XXXX\\PaySlips\\';

var uploadDocument = function(file, appId){

return new Promise((resolve, reject) =>{

jsonObject = JSON.stringify({
  "file" : file
});

var postheaders = {
  'Content-Type' : 'multipart/form-data'
};

const optionsPost = {
  url: 'http://XXXXX/XXXX/XXX/submitAsTransaction/'+appId,
  method: 'POST',
  headers : postheaders,
  body:{
    file: fs.createReadStream(dirName + file),
  }
};

console.log('Options prepared:');
console.log(optionsPost);
console.log('Do the POST call');

var proxyRequest = http.request(optionsPost, function(res) {
console.log("statusCode: ", res.statusCode);

res.on('data', function(d) {
    console.info('POST result:\n');
    process.stdout.write(d);
    console.info('\n\nPOST completed');
  });
});



proxyRequest.write(jsonObject);
proxyRequest.end();
proxyRequest.on('error', function(e) {
  console.error(e);
});
});
}

exports.uploadDocument = uploadDocument;

我正在尝试将文档上传到我可以访问并能够连接的远程服务器上。此外,当我使用邮递员发出请求时,我可以得到期望的正确答复。

拨打电话时,我会在控制台上看到以下内容:-

{ url: 'http://XXXXX/XXXX/XXX/submitAsTransaction/1',
  method: 'POST',
  headers: { 'Content-Type': 'multipart/form-data' },
  body:
  { file:
        ReadStream {
          _readableState: [Object],
          readable: true,
          domain: null,
          _events: [Object],
          _eventsCount: 1,
          _maxListeners: undefined,
          path: 'C:\\Users\\abhinav.a.mehrotra\\Desktop\\DataCap\\PaySlips\\INSURANCE-UK-LIMITED-Payslip---1---Joanna-Jackson.pdf',
          fd: null,
          flags: 'r',
          mode: 438,
          start: undefined,
          end: undefined,
          autoClose: true,
          pos: undefined,
          bytesRead: 0 } } }

对于您可以提供的任何帮助和解决方案,我将不胜感激。我不明白我要去哪里错了。

0 个答案:

没有答案