I am very new to javascript and node js. I am working on uploading image from my iPad app to rest api of node js(express js) using multiparty module.
Here is my image upload code in 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];
Request body data is printed on console.
For getting image on server side I am referring this example. [https://github.com/strongloop/express/blob/master/examples/multipart/index.js][1]
In this example, they are creating var form = new multiparty.Form(); in post method. But I am sending image from application not from browser so I don’t have any form in which input types are defined. How can I write this REST API to get the image and other string parameters?
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);
});
Also I am getting req.body empty. Thanks in Advance!! :)
your objective code is looking perfect. You need to use connect-multiparty module. Here is the sample code to save the file.
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.
});
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With