使用iPhone的POST方法将图像数据发送到服务器

时间:2012-08-10 18:29:19

标签: iphone objective-c ios

显示的是我上传图片到服务器的API。我不知道如何将用户ID和图像数据一起发送到服务器。

url : base_url + /users/editProfilePic
    input: as POST
    userId : user’s id
    picture : base-64 encoded jpg image (300x300px resolution with 1:1 ratio recommended) 
    output: 
    if failed,
    {
       "status": "FAIL",
       "message": "Some Error, try again"
    }

    if success, 
    {
       "status": "OK",
       "message": "Profile pic updated..",
       "url": "http://base_url/images/users/profile_pics/8a21edc7db3f0eab4d6f328e28978aa8.jpg"
    }

任何人都可以通过可以处理此API的代码来帮助我。我是iphone的初学者

我写了一个代码,我在下面给出了无效的代码。

- (BOOL)uploadImage:(NSData *)imageData filename:(NSString *)filename
{
    NSLog(@"Uploading image data %@",imageData);

    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
    DatabaseManger *objDatabase = [[DatabaseManger alloc] init];
    NSMutableDictionary *valDict = [objDatabase getUserDetailsInDictionary];


    NSString *usernameString = [valDict objectForKey:@"UserID"];

    [objDatabase closeSQLDB];
    [objDatabase release];
    [pool release];    NSMutableString *URL = [NSString stringWithFormat:@"%@%@",SERVER_URL,PROFILEPICTURE ]; 
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:URL]];
    NSString *postData = [[NSString alloc] initWithFormat:@"userId=%@&picture=%@",usernameString,imageData];

    NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
        [request setHTTPMethod:@"POST"];
        [request setValue:postLength forHTTPHeaderField:@"Content-Length"];
        [request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
        [request setHTTPBody:[postData dataUsingEncoding:NSUTF8StringEncoding]];
        [postData release];

        NSError *error;
        NSURLResponse *response;
        NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];

        // parse data from sever by JSON

        NSString *data=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding];
        NSDictionary * PostCommentStaus= ((NSDictionary *)[data JSONValue]);
        NSLog(@"%@",PostCommentStaus);
        return ([data isEqualToString:@"OK"]);
        [data release];



    }

0 个答案:

没有答案