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

纵然是瞬间 提交于 2019-12-08 05:50:35

问题


I am to uploading an image file to asp.net Api using 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
}

code works fine but need to upload the image without converting into NSData. Expecting your help Thanks.i found the code above from here

来源:https://stackoverflow.com/questions/17515190/how-upload-image-in-to-asp-net-server-without-converting-into-nsdata-in-iphone

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!