我是javascript和node js的新手。 我正在使用multiparty模块将图像从我的iPad应用程序上传到节点js(表达js)的api。
这是我在目标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空了。 提前致谢!! :)
答案 0 :(得分:4)
您的客观代码看起来很完美。 您需要使用connect-multiparty模块。 以下是保存文件的示例代码。
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.
});
});
答案 1 :(得分:0)
我不知道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 ....
});