AFNetworking上传图片 [英] AFNetworking Uploading Image

查看:181
本文介绍了AFNetworking上传图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看一些例子,但我认为我的问题可能在PHP中。我正在尝试使用AFNetworking从iphone上传图像到服务器。这是我的对象代码:

I've look at a few examples but I think my problem may be with in the PHP. I am trying to upload an image to a server from the iphone using AFNetworking. Here is my obj-c code:

    -(IBAction)uploadButtonClicked:(id)sender
{

NSData *imageToUpload = UIImageJPEGRepresentation(mainImageView.image, 90);
AFHTTPClient *client= [AFHTTPClient clientWithBaseURL:[NSURL URLWithString:@"http://www.THESERVER.com"]];

NSMutableURLRequest *request = [client multipartFormRequestWithMethod:@"POST" path:@"/PROJECT/upload.php" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData: imageToUpload name:@"file" fileName:@"temp.jpeg" mimeType:@"image/jpeg"];
}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSString *response = [operation responseString];
    NSLog(@"response: [%@]",response);
    [MBProgressHUD hideHUDForView:self.view animated:YES];
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    [MBProgressHUD hideHUDForView:self.view animated:YES];
    if([operation.response statusCode] == 403){
        NSLog(@"Upload Failed");
        return;
    }
    NSLog(@"error: %@", [operation error]);

}];

[operation start];
}

这是我的upload.php:

This is my upload.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;

}

当我尝试上传时失败

if (move_uploaded_file($_FILES['file']['tmp_name'], $uploadfile)) {

并返回我设置的403 / false状态代码

and returns the 403 / false status code that I set

推荐答案

这是一个愚蠢的错误...

It was a silly mistake...

在php中我需要

$uploaddir = 'uploads/';

而不是

$uploaddir = '/uploads/';

这篇关于AFNetworking上传图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆