2014-05-10 8 views
6

मुझे एक अजीब समस्या का सामना करना पड़ा। मैं NSURLSession और NSURLSessionDownloadTask का उपयोग कर इंटरनेट से फ़ाइल लोड करता हूं। यहाँ कोडकुलबाइट्सएक्सेटेड टॉवाइट -1 है NSRLSessionDownloadTask

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]; 

मेरी कक्षा 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); 
} 

totalBytesExpectedToWrite हमेशा समान -1 कॉल प्रतिनिधि विधि और मैं उपयोगकर्ता के लिए एक प्रगति दिखाने के लिए है क्योंकि मैं डाउनलोड फ़ाइल के आकार पता नहीं है कोई क्षमता है जब।

क्या आप मुझे बता सकते हैं कि मैंने गलती की है?

+1

यह एक सर्वर समस्या जहां यह ठीक से 'सामग्री-Length' हैडर नहीं भेज रहा है हो सकता है की जरूरत है । यदि आप अपने ब्राउज़र में एक ही यूआरएल का उपयोग करते हैं, तो क्या आपका ब्राउज़र उचित प्रगति दिखाता है? – rmaddy

उत्तर

10

-1NSURLSessionTransferSizeUnknown है, जिसका अर्थ है कि http सर्वर एक "सामग्री-लंबाई" शीर्षक नहीं प्रदान की थी (और डेटा का उपयोग कर भेज दिया जाता है "स्थानांतरण-एन्कोडिंग: chunked")।

शायद ऐसा नहीं है जो आप कर सकते हैं। आप की कोशिश कर सकते, तो https://stackoverflow.com/a/12599242/1187415 से वैकल्पिक हल अपने मामले में काम करता है और साथ ही:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:anURL]; 
[request addValue:@"" forHTTPHeaderField:@"Accept-Encoding"]; 
2

वेब सेवा हेडर फ़ील्ड सामग्री-लंबाई में कुल आकार प्रदान नहीं किया जा सकता।

यदि कुल आकार प्रदान नहीं किया गया है तो आपके ऐप को लंबाई जानने के लिए कोई रास्ता नहीं है और यह एक प्रगति पट्टी प्रदान करता है।

जांचें कि वेब सर्वर से चार्ल्स प्रॉक्सी जैसे विश्लेषक के साथ क्या आ रहा है।

0

सामग्री-लंबाई गैर 0 और totalBytesExpectedToWrite हो सकता है: -1

//TRACK PROGRESS - MOVED DOWN as also used in BACKGROUND REFRESH > DOWNLOAD FILE > CALL DELEGATE 
-(void)URLSession:(NSURLSession *)session 
    downloadTask:(NSURLSessionDownloadTask *)downloadTask 
    didWriteData:(int64_t)bytesWritten 
totalBytesWritten:(int64_t)totalBytesWritten 
totalBytesExpectedToWrite:(int64_t)totalBytesExpectedToWrite 
{ 
    //to see response header 
    NSLog(@"downloadTask.response:%@\n", downloadTask.response); 

// { status code: 200, headers { 
//  "Cache-Control" = "no-cache"; 
//  "Content-Disposition" = "attachment; filename=Directory.zip"; 
//  "Content-Encoding" = gzip; 
//  "Content-Length" = 33666264; 
//  "Content-Type" = "application/octet-stream"; 
//  Date = "Tue, 27 Oct 2015 15:50:01 GMT"; 
//  Expires = "-1"; 
//  Pragma = "no-cache"; 
//  Server = "Microsoft-IIS/8.5"; 
//  "X-AspNet-Version" = "4.0.30319"; 
//  "X-Powered-By" = "ASP.NET"; 
// } } 

    NSDictionary *responseHeaders = ((NSHTTPURLResponse *)downloadTask.response).allHeaderFields; 
    NSString * contentLengthString = responseHeaders[@"Content-Length"]; 
    double contentLengthDouble = 0.0f; 

    if (contentLengthString) { 
     NSNumberFormatter *f = [[NSNumberFormatter alloc] init]; 
     NSNumber *contentLengthNumber = [f numberFromString:contentLengthString]; 
     contentLengthDouble = [contentLengthNumber doubleValue]; 
    }else{ 

    } 
    NSLog(@"contentLengthString:[%@]", contentLengthString); 
    //You can get progress her 

    NSLog(@"bytesWritten:%lld", bytesWritten); 
    NSLog(@"totalBytesWritten:%lld", totalBytesWritten); 

    //DONT USE CAN BE ALWAYS -1 for Gzip 
    NSLog(@"totalBytesExpectedToWrite:%lld", totalBytesExpectedToWrite); 

    //avoid DIV by 0 
    if (contentLengthDouble > 0.0) { 
     double percentage1 = (totalBytesWritten/contentLengthDouble); 
     double percentage = percentage1 * 100.0; 
     NSLog(@"PERCENTAGE DOWNLOADED:[%f%%]", percentage); 
    }else{ 
     NSLog(@"PERCENTAGE DOWNLOADED:[contentLengthDouble is 0]"); 
    } 

    NSLog(@"========="); 
} 

निम्नलिखित अधिक से अधिक उत्पादन के रूप में जिप डाउनलोड किया जाता है।

लेकिन totalBytesExpectedToWrite: -1

तो तुम downloadTask.response

में सामग्री-लंबाई की जाँच करने के
2015-10-27 16:04:18.580 ClarksonsDirectory[89873:15495901] downloadTask.response:<NSHTTPURLResponse: 0x7f9eabaae750> { URL: http://asset10232:50/api/1/dataexport/ios/?lastUpdatedDate=01012014000000 } { status code: 200, headers { 
    "Cache-Control" = "no-cache"; 
    "Content-Disposition" = "attachment; filename=Directory.zip"; 
    "Content-Encoding" = gzip; 
    "Content-Length" = 33666264; 
    "Content-Type" = "application/octet-stream"; 
    Date = "Tue, 27 Oct 2015 16:03:55 GMT"; 
    Expires = "-1"; 
    Pragma = "no-cache"; 
    Server = "Microsoft-IIS/8.5"; 
    "X-AspNet-Version" = "4.0.30319"; 
    "X-Powered-By" = "ASP.NET"; 
} } 

contentLengthString:[33666264] 
bytesWritten:47278 
totalBytesWritten:33606690 
totalBytesExpectedToWrite:-1 
PERCENTAGE DOWNLOADED:[99.823045%] 
संबंधित मुद्दे