Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading image to server via node.js from an iOS app

I am developing an iOS app and I am using node.js for server side scripting. I am facing problem in uploading image to server from iOS app. It is working fine if I am uploading image from webpage form. But if uploading from app side it is not working.

 //test file
 h3 Pic Upload
 form(action='/pic_upload', method='post',enctype='multipart/form-data') 
  | user_pic:
  input(type='file', name='user_pic')
  input(type='submit')

 //app.js
 var userlogin =require('./routes/userlogin');
 app.post('/pic_upload', userlogin.picUpload);

//userlogin.js
//picUpload function
exports.picUpload = function(req, res) {
console.log(req.files);  // showing undefined, when called from IOS app
// pic upload script...
});

I have tried sending image from app side as data or file parameter but it did not work. How to send the file parameter from app side so that I can easily upload the image to server? Kindly suggest a way to tackle the problem.

like image 946
Tirthankar Kundu Avatar asked Sep 04 '13 10:09

Tirthankar Kundu


1 Answers

This code works in my app. Try this.

NSData *imgData = UIImageJPEGRepresentation(newImg, 0.2);

NSString *str = @"displayImage";

    NSString *urlString = @"http://some.url.com/post";

    // create request
    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"];

    // set Content-Type in HTTP header
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary];
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"];

    // post body
    NSMutableData *body = [NSMutableData data];

    // add params (all params are strings)
        [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]];


    // add image data
    if (imgData) {
        [body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
       // [body appendData:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"displayImage\"; filename=\"image.jpg\"\r\n"]];
        [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"; filename=\"image.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]];

    // setting the body of the post to the reqeust
    [request setHTTPBody:body];

    // set URL
    [request setURL:[NSURL URLWithString:urlString]];

    NSString *bodyStr = [[NSString alloc]initWithData:body encoding:NSUTF8StringEncoding];
    NSLog(@" %@",bodyStr);

    NSURLConnection *lot_Connection=[[NSURLConnection alloc]initWithRequest:request delegate:self];

    if(lot_Connection)
    {
        webdata = [[NSMutableData alloc] init];

    }
like image 138
Giya Avatar answered Oct 15 '22 11:10

Giya