我想上傳一個圖像到web服務,但是當我上傳它時,只有圖像的名稱是保存,但不是圖像。圖像不上傳到webservice
請找到供您參考的代碼和圖片。
- (void) imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
{
[self dismissModalViewControllerAnimated:YES];
NSData *image = UIImageJPEGRepresentation([info objectForKey:UIImagePickerControllerOriginalImage], 0.1);
NSMutableString *urlString = [[NSMutableString alloc] initWithFormat:@"name=thefile&&filename=recording"];
[urlString appendFormat:@"%@", image];
NSData *postData = [urlString dataUsingEncoding:NSASCIIStringEncoding
allowLossyConversion:YES];
NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSString *baseurl = @"http://192.168.2.34/Service1.svc/upload/filename.png";
NSURL *url = [NSURL URLWithString:baseurl];
NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:url];
[urlRequest setHTTPMethod: @"POST"];
[urlRequest setValue:postLength forHTTPHeaderField:@"Content-Length"];
[urlRequest setValue:@"application/x-www-form-urlencoded"
forHTTPHeaderField:@"Content-Type"];
[urlRequest setHTTPBody:postData];
NSURLConnection *connection = [NSURLConnection connectionWithRequest:urlRequest delegate:self];
[connection start];
NSLog(@"Started!");
}
上傳圖像的圖像和名稱後顯示如下:
但是當我打開的圖像顯示爲零。
我不知道是怎麼回事wrong..kindly幫助我。
在此先感謝。
很好的答案...很好 – makumar
真棒...它是工作文件..感謝 –
如何在此代碼中上傳視頻 –