Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8428435
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:03:27+00:00 2026-06-10T05:03:27+00:00

I’m having trouble uploading an image with data. This code allows me to upload

  • 0

I’m having trouble uploading an image with data. This code allows me to upload data just fine:

    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",
                        @"1",@"Creator",
                        nil];
[client postPath:@"apidebate/debates" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
    //

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

However, I’ve been unsuccessful in trying to upload an image with that data:

    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];

What in the work could I be doing wrong??

Edit (adding server code)

The back-end is Codeigniter with the REST client Phil Sturgeon built (great library, btw). I’m almost sure it’s not the server code though, as the first line of this method sends me an email. When I attempt to use the above code to post an image, the email never comes. So, it seems it’s nt even getting to the endpoint.

//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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:03:29+00:00Added an answer on June 10, 2026 at 5:03 am

    ok, so apparently using fileName:@"MainMedia" for the filename left me with an extension-less uploaded file which was causing an error server-side. Adding ".jpg" to "MainMedia.jpg" fixed this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I'm having trouble keeping the paragraph square between the quote marks. In firefox the
I have just tried to save a simple *.rtf file with some websites and
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
We're building an app, our first using Rails 3, and we're having to build

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.