2016-12-30 3 views
2

私はiosを新しくしました。 画像や動画をPHPサーバに送る方法。私はキーを持っています"画像" の送信画像とキー"ビデオ"ビデオファイルを送信する。自分のキーでイメージやビデオを送信する必要があります。どうすれば送信できますか?Iosについては、目的のcキーでサーバーに画像やビデオを送信する方法

+1

は[AFNEtworking](https://github.com/AFNetworking/AFNetworking)の顔をしているさ! – Lion

+0

はい、AFNEtworking ..をインポートする方法は? – Sasi

+0

[iOSでのHTTP POSTリクエストの送信]の可能な複製(http://stackoverflow.com/questions/15749486/sending-an-http-post-request-on-ios) – SOFe

答えて

2

Postメソッドを使用してください。また、イメージ/ビデオをデータにアーカイブし、シートデータをサーバーに送信する必要があります。 Alamofireを使ってこれを行うことをお勧めします。これがコードです。あなたがOCに精通しているとして、ここ

let imageData = UIPNGRepresentation(image)! 

Alamofire.upload(imageData, to: "https://httpbin.org/post").responseJSON { response in 
    debugPrint(response) 
} 

the link.

更新ではなくAFNetworkingを使用し、

です。これがコードです。ここで

NSMutableURLRequest *request = [[AFHTTPRequestSerializer serializer] multipartFormRequestWithMethod:@"POST" URLString:@"http://example.com/upload" parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData) { 
     [formData appendPartWithFileURL:[NSURL fileURLWithPath:@"file://path/to/image.jpg"] name:@"file" fileName:@"filename.jpg" mimeType:@"image/jpeg" error:nil]; 
    } 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]; 

the link.

関連する問題