0

AFNetworkを使用して機能するアップロードコードをたくさん検索しましたが、うまくいきませんでした。私は2つの異なるサイトから入手したこのコードを思いつきました:

NSData *imageData = UIImagePNGRepresentation([UIImage imageNamed:@"1.png"]);
NSURL *url = [NSURL URLWithString:@"http://localhost/project"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"upload.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
    [formData appendPartWithFileData:imageData name:@"google" fileName:@"1.png" mimeType:@"image/png"];
}];


AFJSONRequestOperation *operation = [AFJSONRequestOperation JSONRequestOperationWithRequest:request success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) {
    NSLog(@"IP Address: %@", [JSON valueForKeyPath:@"origin"]);
} failure:nil];

[operation start];

しかし、私が取得するのは「IPアドレス:( null)」です。私のphp側はこれです:

function upload(){
$uploaddir = 'uploads/';
$file = basename($_FILES['file']['name']);
$uploadfile = $uploaddir . $file;

if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {
    sendResponse(200, 'Upload Successful');
    return true;
}
sendResponse(403, 'Upload Failed');
    return false;
}

私は本当に少しの助けに感謝します。

4

2 に答える 2

3

私はこれをほんの数日前に作りました。iPhone 側のコード (画像と別のテキスト パラメータ、アイテムをアップロードします)

NSString *uploadURL = @"http://www.baseurl.it/";
NSURL *url = [[NSURL alloc]initWithString:uploadURL];
AFHTTPClient *httpClient = [[AFHTTPClient alloc]initWithBaseURL:url];
NSDictionary *dict = [NSDictionary
                      dictionaryWithObjects:@[self.itemValue]
                      forKeys:@[@"item"]];
// self.itemValue is a post parameter type nsstring

NSURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"path/to/page.php" parameters:dict constructingBodyWithBlock: ^(id <AFMultipartFormData> formData) {
// self.itemData is an NSData containing the image
    if (self.itemData)
        [formData appendPartWithFileData:self.itemData name:@"imageData" fileName:@"imageData.jpg" mimeType:@"image/png"];
}];

// sorry for the formatting here, is a long method using blocks
AFJSONRequestOperation *json = [AFJSONRequestOperation JSONRequestOperationWithRequest:request
                                                                               success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON){
                                                                                   NSLog(@"Upload Success");
                                                                               }                                                                                   
                                    failure:^(NSURLRequest *request, NSHTTPURLResponse *response, NSError *error, id JSON) {
                                                                                   NSLog(@"Error: %@", error.description);

                                                                               }];
[json start];

Php 側:

// file upload
try {
$fileName = $_FILES['imageData']['name'];
$tmpName  = $_FILES['imageData']['tmp_name'];
$fileSize = $_FILES['imageData']['size'];
$fileType = $_FILES['imageData']['type'];

$fp      = fopen($tmpName, 'r');
$content = fread($fp, filesize($tmpName));
$content = addslashes($content);
fclose($fp);

if(!get_magic_quotes_gpc())
{
    $fileName = addslashes($fileName);
}

} catch (Exception $e) {
    echo json_encode("Error:".$e);
}

// $content contains your image

より長いソースから取得したため、エラーがないかどうかはわかりません。そのため、コードの一部を変更しました

于 2012-11-02T21:35:45.860 に答える
0

同様の質問に対する私の回答をご覧ください。このカスタム クラス (私が作成したもの) をすべてのリモート Web サービス操作に使用します。追加の POST パラメータを画像ファイル データと一緒に送信できるほど柔軟です。使用してもしなくてもかまいません。

ここに投稿する

于 2012-11-02T21:43:24.490 に答える