gpt4 book ai didi

ios - NSURLConnection expectedContentLength 返回 -1

转载 作者:可可西里 更新时间:2023-11-01 05:08:46 26 4
gpt4 key购买 nike

我试图在下载文件时查看进度条。该文件是通过 PHP 生成的,我发送的是实际有效的“Content-length” header 。

文件下载正常,所以这不是问题所在。不幸的是,我无法获取文件大小以正确显示进度条。

这是我的代码:

write_to_filename = [issue objectForKeyedSubscript:filename];
NSString *post =[[NSString alloc] initWithFormat:@"user_id=%@&email=%@&password=%@",[userData stringForKey:@"userId"],[userData stringForKey:@"email"],[userData stringForKey:@"password"]];
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://MY_API_REQUEST"]];

NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];

[request setValue:@"application/pdf" forHTTPHeaderField:@"Accept"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPMethod:@"POST"];
[request setHTTPBody:postData];
[NSURLConnection connectionWithRequest:request delegate:self];

[issueArray writeToFile:[self saveFilePath] atomically:YES];

NSURLConnection

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse 

*)response {

NSLog(@"%lld",[response expectedContentLength]);
_responseData = [[NSMutableData alloc] init];

}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[_responseData appendData:data];
}

- (NSCachedURLResponse *)connection:(NSURLConnection *)connection
willCacheResponse:(NSCachedURLResponse*)cachedResponse {

return nil;
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *fileName = [[paths objectAtIndex:0] stringByAppendingPathComponent:write_to_filename];
[_responseData writeToFile:fileName atomically:YES];

write_to_filename = nil;
_responseData = nil;

[[self IssuesOverviewCollection] reloadData];

}

我已经尝试了很多不同的东西,不幸的是它不起作用并且一直返回 -1。

最佳答案

如果您的服务器正在使用压缩(它可以透明地进行压缩),就会发生这种情况。您可以通过更改请求的 Accept-Encoding 参数来关闭服务器上的压缩:

[request setValue:@"identity" forHTTPHeaderField:@"Accept-Encoding"];

关于ios - NSURLConnection expectedContentLength 返回 -1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23834151/

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