gpt4 book ai didi

php - iOS 使用 Post 通过 php 将文件上传到 Web 服务器,如何获取状态?

转载 作者:行者123 更新时间:2023-11-29 10:56:35 24 4
gpt4 key购买 nike

所以我使用 PHP 将图像从 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];

最佳答案

假设您正在使用 UIProgressview:

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

关于php - iOS 使用 Post 通过 php 将文件上传到 Web 服务器,如何获取状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17992244/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com