【问题标题】:iOS Upload a file via php to a Webserver using Post, how can you get the status?iOS 使用 Post 通过 php 将文件上传到 Web 服务器,如何获取状态?
【发布时间】:2013-08-01 11:01:48
【问题描述】:

所以我正在使用 PHP 将图像从 iDevice 上传到网络服务器。我可以以某种方式获取上传的状态(以填充进度条)

这是我的 iDevice 端代码:

NSData *imageData = UIImageJPEGRepresentation(self.bild, 90);

NSString *urlString = @"myurl";
NSLog(urlString);
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"];

NSMutableData *body = [NSMutableData data];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[[NSString stringWithString:[NSString stringWithFormat:@"Content-Disposition: form-data; name=\"userfile\"; filename=\"dernamedesbildes.jpg\"\r\n"]] dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[@"Content-Type: application/octet-stream\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[body appendData:[NSData dataWithData:imageData]];
[body appendData:[[NSString stringWithFormat:@"\r\n--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]];
[request setHTTPBody:body];

NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];

[conn start];

【问题讨论】:

    标签: php ios objective-c progress-bar


    【解决方案1】:

    假设你正在使用 UIProgressview:

    - (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite {
         progressView.progress = (float)totalBytesWritten / totalBytesExpectedToWrite;
     }
    

    【讨论】:

      猜你喜欢
      • 2014-06-04
      • 1970-01-01
      • 2014-03-07
      • 2015-07-21
      • 2020-05-06
      • 1970-01-01
      • 2016-10-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多