使用多方将图像从 ipad 应用程序上传到节点 js 服务器
upload image from ipad app to node js server using multiparty
我对 javascript 和节点 js 很陌生。
我正在使用多方模块从我的 iPad 应用程序上传图像到节点 js(express js)的其余 api。
这是我在 objective c 中的图片上传代码。
NSData *imgData = UIImageJPEGRepresentation(img.image, 0.2);
NSString *urlString = @"http://localhost:3000/api/uploadimage";
NSString *str = @"displayImage";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithFormat:@"---------------------------14737809831464368775746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"currentEventID\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"52344457901000006" dataUsingEncoding:NSUTF8StringEncoding]];
if (imgData) {
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"displayImage\"; filename=\"myimage.jpg\"\r\n"]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"myimage.jpg\"\r\n", str] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imgData];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
[request setURL:[NSURL URLWithString:urlString]];
NSLog(@"HttpBody = %@",request.HTTPBody);
connectionSave = [[NSURLConnection alloc] initWithRequest:request delegate:self];
请求正文数据打印在控制台上。
为了在服务器端获取图像,我指的是这个例子。
[https://github.com/strongloop/express/blob/master/examples/multipart/index.js][1]
在这个例子中,他们正在创建 var form = new multiparty.Form();在 post 方法中。
但是我从应用程序而不是浏览器发送图像,所以我没有定义输入类型的任何形式。我如何编写此 REST API 来获取图像和其他字符串参数?
var express = require('express');
var http = require('http');
var util = require('util')
var multiparty = require('multiparty');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({limit:'10mb', extended: true}));
app.use(bodyParser.json({limit:'10mb'}));
app.post('/api/uploadimage', function(req, res, next){
var form = new multiparty.Form();
console.log(req.body);
});
我也变得 req.body 空了。
提前致谢!! :)
我不知道iOS代码对错,但是在nodejs的情况下,你应该使用multiparty
作为中间件,你可以使用connect-multiparty
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
app.use(require('body-parser').urlencoded({
extended: true
}))
app.post("/api/upload", multipartMiddleware, function(req, res){
files = req.files;
// Do ....
});
您的 objective 代码看起来很完美。
您需要使用连接多方模块。
这是保存文件的示例代码。
app.post('/api/uploadimage', multipartMiddleware, function(req, res) {
console.log(req.body, req.files); // check console
fs.readFile(req.files.urForm-data_name.path, function (err, data) {
//here get the image name and other data parameters which you are sending like image name etc.
fs.writeFile(newPath, data, function (err) {
});
//dont forgot the delete the temp files.
});
});
我对 javascript 和节点 js 很陌生。 我正在使用多方模块从我的 iPad 应用程序上传图像到节点 js(express js)的其余 api。
这是我在 objective c 中的图片上传代码。
NSData *imgData = UIImageJPEGRepresentation(img.image, 0.2);
NSString *urlString = @"http://localhost:3000/api/uploadimage";
NSString *str = @"displayImage";
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData];
[request setHTTPShouldHandleCookies:NO];
[request setTimeoutInterval:30];
[request setURL:[NSURL URLWithString:urlString]];
[request setHTTPMethod:@"POST"];
NSString *boundary = [NSString stringWithFormat:@"---------------------------14737809831464368775746641449"];
NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
[request setValue:contentType forHTTPHeaderField: @"Content-Type"];
NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"currentEventID\"\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"52344457901000006" dataUsingEncoding:NSUTF8StringEncoding]];
if (imgData) {
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// [body appendData:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"displayImage\"; filename=\"myimage.jpg\"\r\n"]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"myimage.jpg\"\r\n", str] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: image/jpeg\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:imgData];
[body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
}
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
[request setURL:[NSURL URLWithString:urlString]];
NSLog(@"HttpBody = %@",request.HTTPBody);
connectionSave = [[NSURLConnection alloc] initWithRequest:request delegate:self];
请求正文数据打印在控制台上。
为了在服务器端获取图像,我指的是这个例子。 [https://github.com/strongloop/express/blob/master/examples/multipart/index.js][1]
在这个例子中,他们正在创建 var form = new multiparty.Form();在 post 方法中。 但是我从应用程序而不是浏览器发送图像,所以我没有定义输入类型的任何形式。我如何编写此 REST API 来获取图像和其他字符串参数?
var express = require('express');
var http = require('http');
var util = require('util')
var multiparty = require('multiparty');
var app = express();
var bodyParser = require('body-parser');
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({limit:'10mb', extended: true}));
app.use(bodyParser.json({limit:'10mb'}));
app.post('/api/uploadimage', function(req, res, next){
var form = new multiparty.Form();
console.log(req.body);
});
我也变得 req.body 空了。 提前致谢!! :)
我不知道iOS代码对错,但是在nodejs的情况下,你应该使用multiparty
作为中间件,你可以使用connect-multiparty
var multipart = require('connect-multiparty');
var multipartMiddleware = multipart();
app.use(require('body-parser').urlencoded({
extended: true
}))
app.post("/api/upload", multipartMiddleware, function(req, res){
files = req.files;
// Do ....
});
您的 objective 代码看起来很完美。 您需要使用连接多方模块。 这是保存文件的示例代码。
app.post('/api/uploadimage', multipartMiddleware, function(req, res) {
console.log(req.body, req.files); // check console
fs.readFile(req.files.urForm-data_name.path, function (err, data) {
//here get the image name and other data parameters which you are sending like image name etc.
fs.writeFile(newPath, data, function (err) {
});
//dont forgot the delete the temp files.
});
});