我要使用下面的 POST 方法代码块将图像文件上传到 asp.net Api...
在uploadViewController.h
IBOutlet UIProgressView *progressIndicator;
IBOutlet UIImageView *imageview;
ASIFormDataRequest *request;
在uploadViewController.m
-(void)upload
{
[request cancel];
double compressionRatio=1;
NSData *imageData = UIImageJPEGRepresentation(imageview.image, 0.9);
while ([imageData length]>50000) {
compressionRatio=compressionRatio*0.5;
imageData=UIImageJPEGRepresentation([imageview image], compressionRatio);
}
NSString *Senderid=@"465465354313";
NSString *ID=@"265446";
NSString *Devtype=@"iphone";
NSURL *url=[NSURL URLWithString:@"http://abc/bcd/Upload.aspx?"];
request=[ASIFormDataRequest requestWithURL:url];
[request setPostValue:ID forKey:@"id"];
[request setPostValue:Devtype forKey:@"Devicetype"];
[request setPostValue:Senderid forKey:@"Senderid"];
[request setTimeOutSeconds:20];
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
[request setShouldContinueWhenAppEntersBackground:YES];
#endif
[request setUploadProgressDelegate:progressIndicator];
[request setDelegate:self];
[request setDidFailSelector:@selector(uploadFailed:)];
[request setDidFinishSelector:@selector(uploadFinished:)];
NSString *path = [NSHomeDirectory() stringByAppendingPathComponent:@"PHOTO"];
[imageData writeToFile:path atomically:YES];
int i;
for (i=0; i<8; i++) {
[request setFile:path forKey:[NSString stringWithFormat:@"file-%i",i]];
}
[request startAsynchronous];
NSLog(@"Uploading data...");
}
- (void)uploadFailed:(ASIHTTPRequest *)theRequest
{
NSLog(@"%@",[NSString stringWithFormat:@"Request failed:\r\n%@",[[theRequest error] localizedDescription]]);
NSLog(@"response----%@",[theRequest responseString]);
}
- (void)uploadFinished:(ASIHTTPRequest *)theRequest
{
NSLog(@"%@",[NSString stringWithFormat:@"Finished uploading %llu bytes of data",[theRequest postLength]]);
NSLog(@"repose-------%@",[theRequest responseString]);
#if __IPHONE_OS_VERSION_MAX_ALLOWED >= __IPHONE_4_0
// Clear out the old notification before scheduling a new one.
if ([[[UIApplication sharedApplication] scheduledLocalNotifications] count] > 0)
[[UIApplication sharedApplication] cancelAllLocalNotifications];
// Create a new notification
UILocalNotification *notification = [[UILocalNotification alloc] init] ;
if (notification) {
[notification setFireDate:[NSDate date]];
[notification setTimeZone:[NSTimeZone defaultTimeZone]];
[notification setRepeatInterval:0];
[notification setSoundName:@"alarmsound.caf"];
[notification setAlertBody:@"Boom!\r\n\r\nUpload is finished!"];
[[UIApplication sharedApplication] scheduleLocalNotification:notification];
}
#endif
}
代码工作正常,但需要上传图像而不转换为 NSData。期待您的帮助谢谢。我从这里找到了上面的代码