Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
257 views
in Technique[技术] by (71.8m points)

iphone - How to upload image to server from gallery as JSON on iOS

How can I upload an image to a server from the gallery on iPhone as JSON? I tried the following:

[Loading startLoading:YES];
NSString *urlString = [NSString stringWithFormat:@"http://railsboxtech.com/singing/jsn_song/register_response.php?accesstoken=%@",Access_Token];
//username, password, name, email, country
NSString *parameter = [NSString stringWithFormat:@"username=%@&password=%@&fname=%@&email=%@&lname=%@&btnImage4=%@",userField.text,passField.text,fnameField.text,emailField.text,lnameField.text,btnImage4];
NSConnection *conn = [[NSConnection alloc] initWithDelegate:self];
[conn sendRequest:urlString withParaMeter:parameter withMethod:@"POST" withTag:1];
[conn startAsynchronousRequest];
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

if you want to upload an image as a string you must convert your image data to Base64, then you can use your above method:-

For converting to a base64 string, refer to this Stack Overflow answer. You can then use your code for uploading.

Another Way

You can directly upload an image like so:

-(void)uploadImage
{       
    NSData *imageData = UIImagePNGRepresentation(yourImage);

    NSString *urlString = [ NSString stringWithFormat:@"http://yourUploadImageURl.php?intid=%@",1];

    NSMutableURLRequest *request = [[[NSMutableURLRequest alloc] init] autorelease];
    [request setURL:[NSURL URLWithString:urlString]];
    [request setHTTPMethod:@"POST"];

    NSString *boundary = [NSString stringWithString:@"---------------------------14737809831466499882746641449"];
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
    [request addValue:contentType forHTTPHeaderField: @"Content-Type"];

    NSMutableData *body = [NSMutableData data];
    [body appendData:[[NSString stringWithFormat:@"
--%@
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name="userfile"; filename="%@"
", 1]] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream

"] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[NSData dataWithData:imageData]];
    [body appendData:[[NSString stringWithFormat:@"
--%@--
",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
    [request setHTTPBody:body];

    [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...