我想上传一个UIImage
到服务器。为此,我使用以下代码行::
- (void)imagePickerController:(UIImagePickerController *)picker
didFinishPickingImage:(UIImage *)image
editingInfo:(NSDictionary *)editingInfo
{
imageView.image = image;
NSData *pngData = UIImagePNGRepresentation(imageView.image);
NSString *imageFile = @"image.png";
NSString *docDir = [NSHomeDirectory() stringByAppendingPathComponent:@"Documents"];
NSString *filePath = [docDir stringByAppendingPathComponent:imageFile];
NSString *dataIS=[NSString base64StringFromData:pngData length:[pngData length]];
[pngData writeToFile:filePath atomically:YES];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[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:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Disposition: form-data; name=\"image\"; filename=\"image.png\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:@"Content-Type: application/octet-stream\r\n\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:pngData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
// setting the body of the post to the reqeust
[request setHTTPBody:body];
NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];
[self.parentViewController dismissModalViewControllerAnimated:YES];
}
但是,我在 gdb 中收到以下错误 ::
Your request could not be processed. Request could not be handled
.
我也对正确使用感到困惑,urlString
即我怀疑我是否正确使用它。
但是,如果我使用以下内容,则urlString
可以将图像文件上传到服务器::
NSString *urlString = [NSString stringWithFormat:@"http://xxxx.com/mediawiki/api.php?action=upload&filename=image.png&url=%@&token=%@", url, token] ;
参考API
for mediawiki
(http://www.mediawiki.org/wiki/API:Upload),有人可以帮我整理一下吗?谢谢并恭祝安康。