ios - 上传图片到服务器时无法显示进度条

标签 ios objective-c image-uploading

我迫切需要显示进度条或事件指示器,因为图片上传需要时间。下面是我的代码,我不明白为什么进度条不显示。我用过 ProgressHUD。

 [ProgressHUD show:@"Please wait..."];
  NSDictionary *params =@{ @"name":self->Name.text, @"contact_no":self->ContactNo.text,@"email_id":self->EmailId.text,@"s_date":Date,@"s_time":Time,@"streat":Street,@"city":City,@"state":State};

 NSData *uploadData = Data;
 NSString *urlString = [NSString stringWithFormat:@"http://fsttest.com/iosservice/supremo/add_supremo"];


 NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
 [request setURL:[NSURL URLWithString:urlString]];
 [request setHTTPMethod:@"POST"];

 NSString *boundary = @"---------------------------14737809831466499882746641449";
 NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@",boundary];
 [request addValue:contentType forHTTPHeaderField: @"Content-Type"];
 NSString *kNewLine = @"\r\n";

 NSMutableData *body = [NSMutableData data];

 for (NSString *name in params.allKeys) {

    NSData *values = [[NSString stringWithFormat:@"%@", params[name]] dataUsingEncoding:NSUTF8StringEncoding];

    [body appendData:[[NSString stringWithFormat:@"--%@%@", boundary, kNewLine] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"%@\"", name] dataUsingEncoding:NSUTF8StringEncoding]];

    [body appendData:[[NSString stringWithFormat:@"%@%@", kNewLine, kNewLine] dataUsingEncoding:NSUTF8StringEncoding]];
    [body appendData:values];
    [body appendData:[kNewLine dataUsingEncoding:NSUTF8StringEncoding]];
}

[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"file_name\"; filename=\"test\"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:uploadData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n",boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];
[request setHTTPBody:body];


NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

最佳答案

选择 1

按照您的方式继续添加委托(delegate)并检查

- (void)connection:(NSURLConnection *)connection   
   didSendBodyData:(NSInteger)bytesWritten
 totalBytesWritten:(NSInteger)totalBytesWritten
totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite    {

    float progress = [[NSNumber numberWithInteger:totalBytesWritten] floatValue];
    float total = [[NSNumber numberWithInteger: totalBytesExpectedToWrite] floatValue];
    NSLog(@"progress/total %f",progress/total);
}

选择 2

将您的请求从 sendSynchronousRequest 修改为 sendAsynchronousRequest for e,g

 //  NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil];
// NSString *returnString = [[NSString alloc] initWithData:returnData encoding:NSUTF8StringEncoding];

像这样调用方法

// set URL
[request setURL:[NSURL URLWithString:baseUrl]];

// add your progress bar here
[ProgressHUD show:@"Please wait..."];

[NSURLConnection sendAsynchronousRequest:request
                               queue:[NSOperationQueue mainQueue]
                   completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

                       NSHTTPURLResponse* httpResponse = (NSHTTPURLResponse*)response;

                        [ProgressHUD dismiss];

                       if ([httpResponse statusCode] == 200) {

                           NSLog(@"success");
                           if ([data length] > 0 && error == nil)
                               [delegate receivedData:data];
                               // hide the progress bar here
                       }

                   }];

关于ios - 上传图片到服务器时无法显示进度条,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42571716/

相关文章:

ios - 如何为uiview的不同部分添加多个UITapGestureRecognizer?

ios - UIViewanimationWithDuration递归导致抖动

ios - UIImage imageNamed : always returns image at 1x

objective-c - 将启用的 NSButton 绑定(bind)到 NSTextView 中的选择

java - 从Java中部署的服务器(即我的Web应用程序)访问用户本地计算机的文件

ruby-on-rails - 载波还是回形针作为上传者?

ios - 在不使用基于 NSUserDefaults 值的 segue 的情况下加载不同的 View

ios - 如何在 swift 3 的 performselector 中发送多个参数?

ios - 隐藏 Firebase 崩溃报告

javascript - 使用 AJAX、PHP 和 jQuery 上传多张图片