2017-04-07 2 views
-1

Je suis Tente de télécharger l'image vers le serveur avec les en-têtes J'espère que mon code est bien, mais je ne sais pas, il ne peut pas télécharger. Mon code estComment télécharger l'image vers le serveur avec en-têtes dans ios

{ 

    NSDictionary *headers = @{ @"content-type":@"multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", 
            @"p-auth-token":self.token}; 

    NSString *urlString = [ NSString stringWithFormat:@"http://ica.com/facilitator/server/v1/media"]; 

    UIImage *image= profile_Image; 
    NSData *imageData = UIImageJPEGRepresentation(image, 0.1); 
    double my_time = [[NSDate date] timeIntervalSince1970]; 
    NSString *imageName = [NSString stringWithFormat:@"%d",(int)(my_time)]; 
    NSString *string = [NSString stringWithFormat:@"%@%@%@", @"Content-Disposition: form-data; name=\"file\"; filename=\"", imageName, @".jpg\"\r\n\""]; 
    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
    [request setURL:[NSURL URLWithString:urlString]]; 
    [request setAllHTTPHeaderFields:headers]; 
    [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:string] 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]; 


    NSURLSession *session = [NSURLSession sharedSession]; 

    [[session dataTaskWithRequest:request completionHandler:^(NSData * _Nullable data, NSURLResponse * _Nullable response, NSError * _Nullable error) { 

     NSDictionary *statusDict = [[NSDictionary alloc]init]; 
     statusDict = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil]; 


     [SVProgressHUD dismiss]; 




     NSLog(@"Images form server %@", statusDict); 



    }] resume]; 
} 

Répondre

2

Essayez AFNetworking avec le code ci-dessous,

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"upload_url" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 

    [formData appendPartWithFileData:data name:@"upload_file_name" fileName:@"somefilename.jpg" mimeType:@"mime_type_of_file"] // you file to upload 

    } error:nil]; 

AFURLSessionManager *manager = [[AFURLSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 

NSURLSessionUploadTask *uploadTask; 
uploadTask = [manager 
    uploadTaskWithStreamedRequest:request 
    progress:^(NSProgress * _Nonnull uploadProgress) { 
    // This is not called back on the main queue. 
    // You are responsible for dispatching to the main queue for UI updates 
    dispatch_async(dispatch_get_main_queue(), ^{ 
    //Update the progress view 
    [progressView setProgress:uploadProgress.fractionCompleted]; 
    }); 
    } 
    completionHandler:^(NSURLResponse * _Nonnull response, id _Nullable responseObject, NSError * _Nullable error) { 
    if (error) { 
    NSLog(@"Error: %@", error); 
    } else { 
    NSLog(@"%@ %@", response, responseObject); 
    } 
    }]; 

[uploadTask resume]; 
+0

où dois-je donner des en-têtes ici? il donne Request failed: erreur non autorisée (401). – Loki

+1

Comme ceci, [formData appendPartWithHeaders: @ {@ "Content-Type": @ "application/x-www-forme-urlencoded; charset = utf-8"} corps: nil]; [formData appendPartWithFileData: nom de données: @ "bytes" nomfichier: @ "newimage.jpeg" mimeType: @ "image/jpeg"]; –