objective-c – AFNetworking将图像从iOS上传到API

前端之家收集整理的这篇文章主要介绍了objective-c – AFNetworking将图像从iOS上传到API前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在使用数据上传图片时遇到问题.这段代码允许我上传数据就好了:
NSURL *url = [NSURL URLWithString:@"my_base_url"];
AFHTTPClient *client = [[AFHTTPClient alloc]initWithBaseURL:url];

//depending on what kind of response you expect.. change it if you expect XML
[client registerHTTPOperationClass:[AFJSONRequestOperation class]];

NSDictionary *params = [[NSDictionary alloc]initWithObjectsAndKeys:
                        _topicText.text,@"Topic",@"1",@"Category",@"",@"MainMedia",@"Creator",nil];
[client postPath:@"apidebate/debates" parameters:params success:^(AFHTTPRequestOperation *operation,id responSEObject) {
    //

} failure:^(AFHTTPRequestOperation *operation,NSError *error) {
    NSLog(@"failure");
}];

但是,我尝试使用该数据上传图像时未成功:

NSURL *url = [NSURL URLWithString:@"my_base_url"];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
NSData *imageData = UIImageJPEGRepresentation([UIImage imageNamed:@"MainMedia"],0.5);
NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"POST" path:@"/upload" parameters:nil constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:imageData name:@"MainMedia" fileName:@"MainMedia" mimeType:@"image/jpeg"];
}];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];
[operation setUploadProgressBlock:^(NSInteger bytesWritten,long long totalBytesWritten,long long totalBytesExpectedToWrite) {
    NSLog(@"Sent %lld of %lld bytes",totalBytesWritten,totalBytesExpectedToWrite);
}];
[operation start];

工作中我能做错什么?

编辑(添加服务器代码)

后端是Codeigniter与REST客户端Phil Sturgeon建立(伟大的图书馆,顺便说一句).我几乎可以肯定它不是服务器代码,因为这个方法的第一行给我发了一封电子邮件.当我尝试使用上面的代码发布图片时,电子邮件永远不会出现.所以,它似乎甚至到达终点.

//CONTROLLER FROM API
function debates_post()
{
    mail('myemailaddress@gmail.com','Test','Posted');
    $tmp_dir = "images/posted/";

    if(isset($_FILES['MainMedia'])){
        $SaniFileName = preg_replace('/[^a-zA-Z0-9-_\.]/','',basename($_FILES['MainMedia']['name']));

        $file = $tmp_dir . $SaniFileName;
        move_uploaded_file($_FILES['MainMedia']['tmp_name'],$file);
    }
    else
        $SaniFileName = NULL;

    $data = array('Topic'=>$this->post('Topic'),'MainMedia'=>$this->post('MainMedia'),'Category'=>$this->post('Category'),'Creator'=>$this->post('Creator'));
    $insert = $this->debate->post_debate($this->post('Topic'),$SaniFileName,$this->post('Category'),$this->post('Creator'));
    if($insert){
        $message = $this->db->insert_id();
    }
    else{
        $message = 'Insert Failed';
    }

    $this->response($message,200);
}

//MODEL
function post_debate($Topic=NULL,$MainMedia='',$Category=NULL,$Creator=NULL){
    $MainMedia = ($MainMedia)?$MainMedia:'';
    $data = array(
                    'Topic' => $Topic,'MainMedia' => $MainMedia,'Category' => $Category,'Creator' => $Creator,'Created' => date('Y-m-d h:i:s')
                );
    return $this->db->insert('debate_table',$data);
}

解决方法

好吧,所以显然使用fileName:@“MainMedia”的文件名留给我一个无扩展名的上传文件,这导致了服务器端的错误.将“.jpg”添加到“MainMedia.jpg”修复此问题.
原文链接:https://www.f2er.com/c/118014.html

猜你在找的C&C++相关文章