2016-03-13 1 views
0

Je souhaite informer mon utilisateur de la progression du téléchargement de certains fichiers Mpeg 3. Juste un. mais je reçois problème comme je le dis dans le titre.- connexion: didSendBodyData: totalBytesWritten: totalBytesExpectedToWrite: ne jamais appeler

Mon code de téléchargement est ici

- (NSString *) upload: (NSData*) postData { 

    NSString *str; 
    NSURL *url = [NSURL URLWithString:@"http://example.com/test.php"]; 



    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:url cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:60.0]; 
    request.HTTPMethod = @"POST"; 


    NSString *boundary = @"unique-consistent-string"; 
    // set Content-Type in HTTP header 
    NSString *contentType = [NSString stringWithFormat:@"multipart/form-data; boundary=%@", boundary]; 
    [request setValue:contentType forHTTPHeaderField: @"Content-Type"]; 


    // post body 
    NSMutableData *body = [NSMutableData data]; 

    // add params (all params are strings) 
    [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

    // add mp3 data 
    if (postData) { 

     [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[[NSString stringWithFormat:@"Content-Disposition: form-data; name=%@; filename=audio\r\n", @"audioFormKey"] dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[@"Content-Type: audio/mpeg3\r\n\r\n" dataUsingEncoding:NSUTF8StringEncoding]]; 
     [body appendData:[[NSString stringWithFormat:@"--%@\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

     [body appendData:postData]; 
     [body appendData:[[NSString stringWithFormat:@"\r\n"] dataUsingEncoding:NSUTF8StringEncoding]]; 
    } 

    [body appendData:[[NSString stringWithFormat:@"--%@--\r\n", boundary] dataUsingEncoding:NSUTF8StringEncoding]]; 

    // setting the body of the post to the reqeust 
    [request setHTTPBody:body]; 

    // set the content-length 
    NSString *postLength2 = [NSString stringWithFormat:@"%lu", (unsigned long)[body length]]; 
    [request setValue:postLength2 forHTTPHeaderField:@"Content-Length"]; 

    NSLog(postLength2); 



    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue currentQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) { 

     if(data.length > 0) 
     { 
      NSError *error; 
      NSURLResponse *response; 
      NSData *urlData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
      NSString *str=[[NSString alloc]initWithData:urlData encoding:NSUTF8StringEncoding]; 

      NSLog(str); 
     } 
    }]; 
    return str; 
} 
- (void)connection:(NSURLConnection *)connection didSendBodyData:(NSInteger)bytesWritten totalBytesWritten:(NSInteger)totalBytesWritten totalBytesExpectedToWrite:(NSInteger)totalBytesExpectedToWrite { 

    NSLog(@"bytes transfer %li" ,(long)bytesWritten); 

} 

Ici, le "transfert des octets" journal est jamais appel.

Merci beaucoup pour le soutien

Répondre

0

Un peu bizarre d'être appeler sendAsynchronousRequest: puis sendSynchronousRequest: dans le gestionnaire d'achèvement?

Dans tous les cas, vous ne configurez pas la connexion URL pour avoir un délégué et, par conséquent, votre méthode de délégation n'est jamais appelée.

Vous devrez allouer et instancier URLConnection, puis le configurer pour utiliser votre objet en tant que délégué. Et il doit se conformer au protocole NSURLConnectionDelegate.

+0

Fonctionne avec mise à jour Méthode avec NSURLConnection * connexion = [[NSURLConnection alloc] initWithRequest: demande delegate: self startImmediately: NO]; [connexion scheduleInRunLoop: [NSRunLoop mainRunLoop] forMode: NSDefaultRunLoopMode]; [début de la connexion]; – user3279327

+0

@ user3279327 Excellent! Maintenant, vous avez toujours cette requête synchrone incorporée dans une requête asynchrone. Cela semble vouloir télécharger la demande deux fois, ce qui n'est probablement pas ce que vous voulez? – bbum