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

后端 未结 1 1355
野性不改
野性不改 2021-02-06 10:26

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

相关标签:
1条回答
  • 2021-02-06 10:53

    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];
    
        }
    
    0 讨论(0)
提交回复
热议问题