2013-04-26 55 views
0

我正在使用wordpress作为移动应用程序的web服务器(在我的情况下是iOS), 但我想问一下用户如何在移动应用程序中添加/编辑个人资料图片,我正在考虑使用JSON发送图片文件,我做了一些研究,发现了类似gravatar.com的东西,可用于添加个人资料图片,但我的问题是用户如何从移动应用程序添加/编辑?在wordpress中使用JSON的个人资料图片

ps:移动应用程序基于目标c + JSON构建。

谢谢。

回答

0

您需要创建一个http post请求并添加图像作为请求的主体。

与此类似

NSString *boundary = @"AaB03x"; 
NSMutableData *postbody = [NSMutableData data]; 

[postbody appendData:[[NSString stringWithFormat:@"--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

[postbody appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"imageFile.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postbody appendData:[[NSString stringWithFormat:@"Content-Type: %@\r\n\r\n",IMAGE_MIME_TYPE] dataUsingEncoding:NSUTF8StringEncoding]]; 
[postbody appendData:[NSData dataWithContentsOfFile:@"pass the file path here"]]; 
[postbody appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

//////////////////////////////PREPARING POST REQUEST BODY///////////////////////////////////////////// 

NSMutableURLRequest *uploadRequest = [[NSMutableURLRequest alloc] 
             initWithURL:[NSURL URLWithString:@"url to server"] 
             cachePolicy: NSURLRequestReloadIgnoringLocalCacheData 
             timeoutInterval:45]; 

[uploadRequest setHTTPMethod:@"POST"]; 
[uploadRequest setValue:@"multipart/form-data; boundary=AaB03x" forHTTPHeaderField:@"Content-Type"]; 
[uploadRequest setHTTPBody:postbody]; 
相关问题