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 8589571
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T23:05:23+00:00 2026-06-11T23:05:23+00:00

Right now in my rails app I’m using Carrierwave to upload files to Amazon

  • 0

Right now in my rails app I’m using Carrierwave to upload files to Amazon S3. I’m using a file selector and a form to select and submit the file, this works well.

However, I’m now trying to make posts from an iPhone app and am receiving the contents of the file. I’d like to create a file using this data and then upload it using Carrierwave so that I can get the correct path back.

May file model consists of:

path
file_name
id
user_id

where path is the Amazon S3 url. I’d like to do something like this to build the files:

 data = params[:data]
~file creation magic using data~
~carrierwave upload magic using file~
@user_id = params[:id]
@file_name = params[:name]
@path = path_provided_by_carrierwave_magic
File.build(@user_id, @file_name, @path)

Would really love someone to point me in the right direction. Thanks!

  • 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-11T23:05:24+00:00Added an answer on June 11, 2026 at 11:05 pm

    Alright, I have a working solution. I’m going to best explain what I did so that others can learn from my experience. Here goes:

    Assuming you have an iPhone app that takes a picture:

    //handle the image that has just been selected
    - (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary *)info
    {
        //get the image
        UIImage* image = [info valueForKey:@"UIImagePickerControllerOriginalImage"];
    
        //scale and rotate so you're not sending a sideways image -> method provided by http://blog.logichigh.com/2008/06/05/uiimage-fix/
        image = [self scaleAndRotateImage:image];
    
        //obtain the jpeg data (.1 is quicker to send, i found it better for testing)
        NSData *imageData = [NSData dataWithData:UIImageJPEGRepresentation(image, .1)];
    
        //get the data into a string
        NSString* imageString = [NSString stringWithFormat:@"%@", imageData];
        //remove whitespace from the string
        imageString = [imageString stringByReplacingOccurrencesOfString:@" " withString:@""];
        //remove < and > from string
        imageString = [imageString substringWithRange:NSMakeRange(1, [imageString length]-2)];
    
        self.view.hidden = YES;
        //dismissed the camera
        [picker dismissModalViewControllerAnimated:YES];
    
        //posts the image
        [self performSelectorInBackground:@selector(postImage:) withObject:imageString];
    }
    
    - (void)postImage:(NSString*)imageData
    {
        //image string formatted in json
       NSString* imageString = [NSString stringWithFormat:@"{\"image\": \"%@\", \"authenticity_token\": \"\", \"utf8\": \"✓\"}", imageData];
    
        //encoded json string
        NSData* data = [imageString dataUsingEncoding:NSUTF8StringEncoding];
    
        //post the image
        [API postImage:data];
    }[/code]
    
    Then for the post:
    
    [code]+(NSArray*)postImage:(NSData*) data
    {
        //url that you're going to send the image to
        NSString* url = @"www.yoururl.com/images";
    
        //pretty self explanatory request building
        NSMutableURLRequest* request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:url]];
    
        [request setTimeoutInterval:10000];
    
        [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    
        [request setHTTPMethod: @"POST"];
    
        [request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
    
        [request setHTTPBody:data];
    
        NSError *requestError;
        NSURLResponse *urlResponse = nil;
    
        NSData *result = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&requestError];
    
        return [API generateArrayWithData:result];
    }
    

    On the rails side I set up a method specifically for handling mobile images, this should help you post the image to your Amazon S3 account through Carrierwave:

    def post
          respond_to do |format|
      format.json {
     #create a new image so that you can call it's class method (a bit hacky, i know)
      @image = Image.new
    #get the json image data
      pixels = params[:image]
    #convert it from hex to binary
    pixels = @image.hex_to_string(pixels)
    #create it as a file
       data = StringIO.new(pixels)
    #set file types
        data.class.class_eval { attr_accessor :original_filename, :content_type }
        data.original_filename = "test1.jpeg"
        data.content_type = "image/jpeg"
    #set the image id, had some weird behavior when i didn't
        @image.id = Image.count + 1
    #upload the data to Amazon S3
      @image.upload(data)
    #save the image
      if @image.save!
      render :nothing => true
      end
      }
      end
      end
    

    This works for me for posting and I feel should be pretty extendable. For the class methods:

    #stores the file
      def upload(file)
      self.path.store!(file)
      end
    
    #converts the data from hex to a string -> found code here http://4thmouse.com/index.php/2008/02/18/converting-hex-to-binary-in-4-languages/
      def hex_to_string(hex)
        temp = hex.gsub("\s", "");
        ret = []
        (0...temp.size()/2).each{|index| ret[index] = [temp[index*2, 2]].pack("H2")}
        file = String.new
        ret.each { |x| file << x}
        file  
      end
    

    Not saying this code is perfect, not even by a longshot. However, it does work for me. I’m open to suggestions if anyone thinks it could be improved. Hope this helps!

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

Sidebar

Related Questions

Right now in my Rails 3 app I'm rendering partials using rjs in my
Right now I have an upload field while uploads files to the server. The
I have a Rails app that uses MySQL, MongoDB, NodeJS (and SocketIO). Right now,
I'm building a Rails 3 app on Heroku. Right now my error pages and
I have huge Rails app on development right now, which run VERY slow on
Im trying to update someone else's Rails app. Right now, an HTML table displays
I have a rails app hosted on www.figurella.com.do, and Right now the wordpress blog
I am working on building a rails app and right now I am struggling
I'm building my first rails app just right now and now I have a
I'm developing in rails right now and I was wondering if there are any

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.