How to upload image to asp.net server without converting to NSData on iPhone

I upload an image file to asp.net Api using the POST method code block below ...

in uploadViewController.h

IBOutlet UIProgressView *progressIndicator; IBOutlet UIImageView *imageview; ASIFormDataRequest *request; 

in 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 } 

works fine, but you need to upload the image without conversion to NSData. Awaiting your help, I found the code above from here

+1
source share

Source: https://habr.com/ru/post/959446/


All Articles