-(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
if([recievedData length]) [ recievedData setLength:0 ];
download_size =[response expectedContentLength];
}
Run Code Online (Sandbox Code Playgroud)
我有这个代码.download_size是NSInteger.expectedContentLenght总是返回:-1.也许有人知道为什么?我尝试使用很长时间,但效果是一样的.
感谢帮助.
我遇到了一个奇怪的问题.我使用NSURLSession和从Internet加载文件NSURLSessionDownloadTask.这是代码
NSURLSessionConfiguration *sessionConfiguration =
[NSURLSessionConfiguration backgroundSessionConfiguration:kSessionId];
self.session = [NSURLSession sessionWithConfiguration:sessionConfiguration
delegate:self
delegateQueue:[NSOperationQueue new]];
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *request = [NSURLRequest requestWithURL:url];
NSURLSessionDownloadTask *downloadTask = [self.session downloadTaskWithRequest:request];
[downloadTask resume];
Run Code Online (Sandbox Code Playgroud)
我的班级被宣布为NSURLSessionDownloadDelegate,我得到了很好的回调.但是当系统调用委托方法时
- (void)URLSession:(NSURLSession *)session downloadTask:(NSURLSessionDownloadTask *)downloadTask didWriteData:(int64_t)bytesWritten totalBytesWritten:(int64_t)totalBytesWritten totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite
{
NSLog(@"totalBytesExpectedToWrite: %lld", totalBytesExpectedToWrite);
NSLog(@"%lld", totalBytesWritten);
}
Run Code Online (Sandbox Code Playgroud)
totalBytesExpectedToWrite总是相等-1,我没有能力向用户显示进度,因为我不知道下载文件的大小.
你能告诉我哪里弄错了吗?
我在服务器端获得的请求缺少content-length每个多部分的请求.
我试图更改标题但仍然无法显示.
我使用Alamofire/multipart form-data成功地将图像文件和一些数据从设备/模拟器发送到服务器.
var parameters = […]
let url = try! URLRequest(url: “URL”, method: .post, headers: ["Content-type": "multipart/form-data"])
Alamofire.upload(multipartFormData: { (multipartFormData) in
for (key, value) in parameters {
multipartFormData.append((value.data(using: String.Encoding.utf8)!), withName:key)
}
multipartFormData.append(UIImageJPEGRepresentation(image!, 0.5)!, withName: “Image”, mimeType: "image/jpeg")
}, to: "\(url)" , encodingCompletion: {(encodingResult) in
switch encodingResult {
case .success(let upload, _, _):
//print(result)
upload.uploadProgress(closure: { (Progress) in
self.progressView.progress = Float(Progress.fractionCompleted)
print("Upload Progress: \(Progress.fractionCompleted)")
})
print("REQUEST = \(request)")
print(encodingResult)
upload.responseJSON { response in
//self.delegate?.showSuccessAlert()
print( …Run Code Online (Sandbox Code Playgroud) multipartform-data content-length http-headers swift alamofire
我正在尝试在下载文件时查看进度条.该文件是通过PHP生成的,我发送的是"Content-length"标题,实际上可以正常工作.
文件下载正常,这不是问题.不幸的是,我无法获取文件大小以便正确显示进度条.
这是我的代码:
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];
Run Code Online (Sandbox Code Playgroud)
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 …Run Code Online (Sandbox Code Playgroud) ios ×3
alamofire ×1
connection ×1
http-headers ×1
ipad ×1
iphone ×1
nsurlsession ×1
progress-bar ×1
swift ×1