Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uploading Image via POST in Objective C

I'm currently working on uploading an image to a server via HTTP Post and can't seem to figure out a way to build the url that calls the service. The user selects an image from the library or camera and then calls a json service that performs the insert statement.

The service is expecting the following uritemplate:

@"%@/DataTransfer/SetUserProfileImage?EMP_ID=%@&image=%@&imageName=%@"

It is expecting that the image data is converted somehow to string and sent over url.

This is my current code:

- (BOOL)setUserProfileImage:(UIImage *)imgUser Name:(NSString *)strName{

    AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

    NSData *dataImage = UIImagePNGRepresentation(imgUser);

    NSString* theNSString = [[NSString alloc] initWithData:dataImage encoding:NSASCIIStringEncoding];

    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"%@/DataTransfer    /SetUserProfileImage?EMP_ID=%@&"
                                   "image=%@&imageName=%@",
                                   appDelegate.ServerAddress, 
                                   appDelegate.UserId,
                                   theNSString,
                                   strName]];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url 
                                                       cachePolicy: NSURLRequestUseProtocolCachePolicy
                                                   timeoutInterval:10.0];

    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];


    NSURLResponse* response = nil;
   NSError* resultError = nil;

   NSData* data = [NSURLConnection sendSynchronousRequest:request 
                                     returningResponse:&response 
                                                 error:&resultError];

   NSString *strResult = [[NSString alloc] initWithBytes:[data bytes] 
                                               length:[data length] 
                                             encoding:NSUTF8StringEncoding]; 

   BOOL imgResponse = [strResult boolValue];
   [strResult release];

   return imgResponse;
}

I get an error saying that the NSURL is "". Can't seem to build a correct URL. I know that the service itself converts this string to an image again.

UPDATE:

- (BOOL)setUserProfileImage:(UIImage *)imgUser Name:(NSString *)strName{

AppDelegate *appDelegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];

NSString *url = [NSString stringWithFormat:@"%@/DataTransfer/SetUserProfileImage",appDelegate.ServerAddress]; 

NSData *data = UIImagePNGRepresentation(imgUser);

NSString * boundary = @"tweetPhotoBoundaryParm";
NSMutableData *postData = [NSMutableData dataWithCapacity:[data length] + 1024];

name=\"EMP_ID\"\r\n\r\n%@", @"100-01"];
NSString * boundaryString = [NSString stringWithFormat:@"\r\n--%@\r\n", boundary];
NSString * boundaryStringFinal = [NSString stringWithFormat:@"\r\n--%@--\r\n", boundary];


[postData appendData:[boundaryString dataUsingEncoding:NSUTF8StringEncoding]];    
[postData appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"image\";\r\nfilename=\"media.png\"\r\nContent-Type: image/png\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[postData appendData:data];
[postData appendData:[boundaryStringFinal dataUsingEncoding:NSUTF8StringEncoding]];

NSMutableURLRequest * theRequest=(NSMutableURLRequest*)[NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] 
                                                                            cachePolicy:NSURLRequestUseProtocolCachePolicy 
                                                                        timeoutInterval:60.0];

[theRequest setHTTPMethod:@"POST"];

[theRequest addValue:[NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary] forHTTPHeaderField:@"Content-Type"];
//[theRequest addValue:@"www.tweetphoto.com" forHTTPHeaderField:@"Host"];
NSString * dataLength = [NSString stringWithFormat:@"%d", [postData length]];
[theRequest addValue:dataLength forHTTPHeaderField:@"Content-Length"];
[theRequest setHTTPBody:(NSData*)postData];

NSURLConnection * theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];

[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
if (theConnection)
{
    webData =[[NSMutableData data] retain];
}
else
{
    NSLog(@"%@",@"Could not connect to the network");
}

return false;
}
like image 591
Angie Avatar asked Dec 27 '22 22:12

Angie


2 Answers

Objective-C

-(void)saveImageToServer
{
    // COnvert Image to NSData
    NSData *dataImage = UIImageJPEGRepresentation([UIImage imageNamed:@"yourImage"], 1.0f);

    // set your URL Where to Upload Image
    NSString *urlString = @"Your URL HERE";

    // set your Image Name
    NSString *filename = @"YourImageFileName";

    // Create 'POST' MutableRequest with Data and Other Image Attachment.
    NSMutableURLRequest* request= [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];
    NSString *boundary = @"---------------------------14737809831466499882746641449";
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
    NSMutableData *postbody = [NSMutableData data];
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"%@.jpg\"\r\n", filename] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
    [postbody appendData:[NSData dataWithData:dataImage]];
    [postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:postbody];

    // Get Response of Your Request
    NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
    NSString *responseString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
    NSLog(@"Response  %@",responseString);
}

Swift

// COnvert Image to NSData
var dataImage: NSData = UIImageJPEGRepresentation(UIImage(named: "yourImage"), 1.0)
// set your URL Where to Upload Image
var urlString: String = "Your URL HERE"
// set your Image Name
var filename: String = "YourImageFileName"
// Create 'POST' MutableRequest with Data and Other Image Attachment.
var request: NSMutableURLRequest = NSMutableURLRequest()
request.URL = NSURL(string: urlString)!
request.HTTPMethod = "POST"
var boundary: String = "---------------------------14737809831466499882746641449"
var contentType: String = "multipart/form-data; boundary=\(boundary)"
request.addValue(contentType, forHTTPHeaderField: "Content-Type")
var postbody: NSMutableData = NSMutableData.data()
postbody.appendData("\r\n--\(boundary)\r\n".dataUsingEncoding(NSUTF8StringEncoding))
postbody.appendData("Content-Disposition: form-data; name=\"userfile\"; filename=\"\(filename).jpg\"\r\n".dataUsingEncoding(NSUTF8StringEncoding))
postbody.appendData(String.stringWithString("Content-Type: application/octet-stream\r\n\r\n").dataUsingEncoding(NSUTF8StringEncoding))
postbody.appendData(NSData.dataWithData(dataImage))
postbody.appendData("\r\n--\(boundary)--\r\n".dataUsingEncoding(NSUTF8StringEncoding))
request.HTTPBody = postbody
// Get Response of Your Request
var returnData: NSData = NSURLConnection.sendSynchronousRequest(request, returningResponse: nil, error: nil)
var responseString: String = String(data: returnData, encoding: NSUTF8StringEncoding)
NSLog("Response  %@", responseString)
like image 125
Dipen Panchasara Avatar answered Jan 14 '23 12:01

Dipen Panchasara


This approach has several problems.

1) Converting the raw bytes of a image into a string will never work.

NSData *dataImage = UIImagePNGRepresentation(imgUser);
NSString* theNSString = [[NSString alloc] initWithData:dataImage encoding:NSASCIIStringEncoding];

Will fail. You will never be able to reconstruct dataImage from theNSString. You need to base 64 encode dataImage. Use something like this to do the base 64 encoding.

2) Don't put image data into a URL. You need to put the image data in a post body.

3) Don't use "application/x-www-form-urlencoded", use "multipart/form-data".


Updated after Comments

Sorry, but it looks like you have a lot of work to do in understanding your system.

In the example above, you added all the data to the URL query string, but did not add anything to the body of the message. In that example you set the content type to be "application/x-www-form-urlencoded".

Now you seem to think the POST needs to be in JSON. You need to find out how that should be done. How does that JSON message need to be attached? Before you said the data needed to be in the URL, is this still the case? If you need to attach the JSON message to the POST body, what does the content type of the POST need to be? What is the structure of the JSON message (all the key/value fields needed)?

Before anyone can help you, you need to find out exactly what's needed. There should be someone who can give you an accurate description of the HTTP message.

like image 22
Jeffery Thomas Avatar answered Jan 14 '23 13:01

Jeffery Thomas