我使用节点js与快速框架和休息api
对于休息api客户端我使用Chrome浏览器的邮递员扩展
在这里,我能够从选项“x-www-form-urlencoded”获取值,但我无法从“表单数据”获取值我想从“表单数据”选项中获取值,还需要上传图像文件.
请帮助我实现这一目标.我想从“表单数据”选项和图像中获取值.请帮我.
下面我提到了我用过的代码.
对于休息api客户端我使用Chrome浏览器的邮递员扩展
在这里,我能够从选项“x-www-form-urlencoded”获取值,但我无法从“表单数据”获取值我想从“表单数据”选项中获取值,还需要上传图像文件.
请帮助我实现这一目标.我想从“表单数据”选项和图像中获取值.请帮我.
下面我提到了我用过的代码.
var express = require('express'); var path = require('path'); var favicon = require('static-favicon'); var logger = require('morgan'); var cookieParser = require('cookie-parser'); var http = require('http').Server(app); var MysqL = require('MysqL'); var util = require('util'); var trim = require('trim'); var validator = require('validator'); var bodyParser = require('body-parser'); var Ingest = require('ingest'); var multer = require('multer'); var upload = multer({ dest: 'uploads/' }); var type = upload.single('recfile'); passport = require('passport'),LocalStrategy = require('passport-local').Strategy; async = require('async'); app.use(bodyParser.json()); app.use(bodyParser.urlencoded({ extended: true })); app.post('/upload',function(req,res){ console.log(req.file); // "form-data" values not able to get here console.log(req);// "form-data" values not able to get here console.log('body : '+JSON.stringify(req.body));// "form-data" values not able to get here });
app.post('/upload',res){ console.log('req.headers \n '+JSON.stringify(req.headers)); console.log('req.body.file :- '+req.body.file); console.log('\n\n req.body :- '+JSON.stringify(req.body)); });
我得到了上面代码的以下结果.
req.headers
{"host":"localhost:3001","connection":"keep-alive","content length":"5808","cache-control":"no-cache","origin":"chrome-extension://mkhojklkhkdaghjjfdnphfphiaiohkef","password":"password","user-agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML,like Gecko) Chrome/49.0.2623.87 Safari/537.36","username":"User2","content-type":"multipart/form-data; boundary=----WebKitFormBoundaryV4zAIbjEyKYxLRWe","accept":"*/*","accept-encoding":"gzip,deflate","accept-language":"en-US,en;q=0.8","cookie":"connect.sid=s%3Atz4f1ZgJkaAjuDD1sOkMB9rr.Z8EUIyxEcr0EyFQL96v0ExGRidM3SAVTx8IIr52O0OI"}
req.body.file: – undefined
req.body: – {}
解决方法
我在下面的代码的帮助下得到了解决方案
var express = require('express'); var router = express.Router(); var util = require("util"); var fs = require("fs"); var formidable = require('formidable'); var path = require('path'); router.post("/upload",res,next){ var form = new formidable.IncomingForm(); form.parse(req,function(err,fields,files) { // `file` is the name of the <input> field of type `file` console.log(files); console.log(fields); res.writeHead(200,{'content-type': 'text/plain'}); res.write('received upload:\n\n'); res.end(util.inspect({fields: fields,files: files})); }); form.on('error',function(err) { console.error(err); }); form.on('progress',function(bytesReceived,bytesExpected) { var percent_complete = (bytesReceived / bytesExpected) * 100; console.log(percent_complete.toFixed(2)); }); form.on('end',function(fields,files) { /* Temporary location of our uploaded file */ var temp_path = this.openedFiles[0].path; /* The file name of the uploaded file */ var file_name = this.openedFiles[0].name; /* Location where we want to copy the uploaded file */ var new_location = 'public/images/'; fs.readFile(temp_path,data) { fs.writeFile(new_location + file_name,data,function(err) { fs.unlink(temp_path,function(err) { if (err) { console.error(err); } else { console.log("success!"); } }); }); }); }); });
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。