如何从 iOS App 上传图片到 node.js?
How to upload image to node.js from iOS App?
iOS代码,我要用
服务器是Node.js&express,upload.js路径在./routes。我希望使用多方。但是 node-multiparty/examples/upload.js 不是 express 。谁能帮帮我?
代码与示例中给出的代码非常相似。您只需将 form.parse 放在路线中,就像本例中的
var path = require('path');
var express = require('express');
var multiparty = require('multiparty');
var util = require('util');
var app = express();
/*var staticPath = path.resolve(__dirname, '/public');
app.use(express.static(staticPath));*/
//Simulating web server
app.get('/uploadImage', function (req, res) {
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form action="/uploadImage" enctype="multipart/form-data" method="post">'+
'<input type="text" name="title"><br>'+
'<input type="file" name="upload" multiple="multiple"><br>'+
'<input type="submit" value="Upload">'+
'</form>'
);
})
//This routes matters
app.post('/uploadImage', function (req, res) {
// parse a file upload
var form = new multiparty.Form();
//After your request body is parsed with multipart/form encoding
//You can access to the files and the fields in that form
//You may upload it to another image service
form.parse(req, function (err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
//The property that matter inside files object it's path
//With that, you can write it in your server file system (fs)
//or data base or cloud service
console.log('files received', files);
console.log('fields received', fields);
res.end(util.inspect({fields: fields, files: files}));
});
})
app.listen(3000, function() {
console.log('listening');
});
确保安装所有依赖项
npm install express 多方
iOS代码,我要用
服务器是Node.js&express,upload.js路径在./routes。我希望使用多方。但是 node-multiparty/examples/upload.js 不是 express 。谁能帮帮我?
代码与示例中给出的代码非常相似。您只需将 form.parse 放在路线中,就像本例中的
var path = require('path');
var express = require('express');
var multiparty = require('multiparty');
var util = require('util');
var app = express();
/*var staticPath = path.resolve(__dirname, '/public');
app.use(express.static(staticPath));*/
//Simulating web server
app.get('/uploadImage', function (req, res) {
res.writeHead(200, {'content-type': 'text/html'});
res.end(
'<form action="/uploadImage" enctype="multipart/form-data" method="post">'+
'<input type="text" name="title"><br>'+
'<input type="file" name="upload" multiple="multiple"><br>'+
'<input type="submit" value="Upload">'+
'</form>'
);
})
//This routes matters
app.post('/uploadImage', function (req, res) {
// parse a file upload
var form = new multiparty.Form();
//After your request body is parsed with multipart/form encoding
//You can access to the files and the fields in that form
//You may upload it to another image service
form.parse(req, function (err, fields, files) {
res.writeHead(200, {'content-type': 'text/plain'});
res.write('received upload:\n\n');
//The property that matter inside files object it's path
//With that, you can write it in your server file system (fs)
//or data base or cloud service
console.log('files received', files);
console.log('fields received', fields);
res.end(util.inspect({fields: fields, files: files}));
});
})
app.listen(3000, function() {
console.log('listening');
});
确保安装所有依赖项 npm install express 多方