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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T03:49:02+00:00 2026-06-09T03:49:02+00:00

I’m uploading a pdf file from an iOS application to a Rails application. The

  • 0

I’m uploading a pdf file from an iOS application to a Rails application.
The file is uploaded, but its content type gets corrupted.

Here is the relevant part of the iOS code to upload the file (using AFNetworking library):

NSURL *url = [NSURL URLWithString:kWebserviceHost];
AFHTTPClient *httpClient = [[AFHTTPClient alloc] initWithBaseURL:url];
//    httpClient.parameterEncoding = AFFormURLParameterEncoding; // Experimented with different values, no effect

NSData *documentData = [NSData dataWithContentsOfURL:self.documentURL];
if (! documentData) {
    NSLog(@"Trying to upload nil document: sorry! - %@", self.documentData);
    return;
}

NSMutableURLRequest *request = [httpClient multipartFormRequestWithMethod:@"PUT" // @"POST" 
                                                                     path:@"new_upload"
                                                               parameters:@{ @"fooKey" : fooString, @"barKey" : barString }
                                                constructingBodyWithBlock: ^(id <AFMultipartFormData>formData) {
    [formData appendPartWithFileData:documentData name:@"document" fileName:@"upload.pfd" mimeType:@"application/pdf"];
}];


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

[operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
    uploadSuccessBlock();
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"Sorry, could not upload document - %@", error);
    failureBlock(error);
}];
[operation start];

Let’s go to the other side, a Rails 3.2.3 server.
I’m using the gem PaperClip (version 3.1.4). Here is the definition of the paperclip attachment in my model class:

paperclip_storage = (Rails.env.production?) ? {
                       :storage        => :s3,
                       :s3_credentials => "#{Rails.root}/config/s3.yml",
                       :path           => ':attachment/:id/:style.:extension',
                       :bucket         => 'mybucketname' #,
                       # :s3_headers =>  { "Content-Type" => "video/mp4" }
                        } 
                     : { :headers =>  { "Content-Type" => "application/pdf" }
                     }

has_attached_file :document, paperclip_storage.merge( :use_timestamp => false,
  :url  => "/assets/:id/:basename.:extension",
  :path => ":rails_root/public/assets/:id/:basename.:extension",
  :processors => nil # Maybe the processing of the file mess things up?
)

I also tried to skip the post process (even though the documentation says none should be applied because no style is defined).

before_post_process :skip_post_process

def skip_post_process
  return false
end

An other try: setting the content_type in a post_process filter:

after_post_process :force_content_type_to_pdf
 def force_content_type_to_pdf
   puts "--------------- Changing content_type"
   self.document.instance_write(:content_type, "application/pdf")
 end

Here is the controller method to receive and save the file:

def new_upload

  @document = Document.create()
  document = params[:document]

  if (document.nil?) 
    return
  end

  @document.document = document

  puts "Document type: #{@document.document.content_type}" # logs: 'application/pdf'
  puts "Parameters: #{params}" # logs     Parameters: {"fooKey"=>"foo", "barKey"=>"bar", "document"=>#<ActionDispatch::Http::UploadedFile:0x007ff5a429be58 @original_filename="upload.pfd", @content_type="application/pdf", @headers="Content-Disposition: form-data; name=\"document\"; filename=\"upload.pfd\"\r\nContent-Type: application/pdf\r\n", @tempfile=#<File:/var/folders/vy/zm_x1bts5hs6pkvzk7clnmvm0000gn/T/RackMultipart20120802-12714-1j0hq6q>>}


  @document.foo = params['fooKey']
  @document.bar = params['barKey']

  if    @document.save
    render :json => {status: "saved" }
    return
  else 
      render json: @document.errors, status: :unprocessable_entity 
      return
  end
end

The file is uploaded, but unreadable. The Finder does not know which application to use to open it.
Here is a screenshot of a QuickLook preview:
QuickLooking the file

The content type is somehow mixed up.
file --mime /file/path/before/or/after/upload.pdf returns the following message on the original file (the one that will be uploaded by the iOS app) or on the file created by the Rails server after a successful upload:
/upload.pfd: application/pdf; charset=binary. Sounds good so far.

mdls -name kMDItemContentType /file/path/before/upload.pdf returns kMDItemContentType = "com.adobe.pdf" on the file to be uploaded. Still ok.
But the same command on the file created by the Rails server returns: kMDItemContentType = "dyn.ah62d4rv4ge81a3xe".
This at least explains why the Finder is confused.

The problem is similar when downloading the file in a browser. Here is the relevant method of my controller:

def download_document
  @document = Document.find(params[:id])

  if (@document && @document.document) 
    # Line below propagate the file content-type problem:
    send_file Rails.root.join(document.path), :type => "application/pdf", :x_sendfile => false, :stream => false

    # This hack works while server is locally hosted
    send_data File.read(Rails.root.join(@document.document.path)),  :type => "application/pdf"
  end
end

Unfortunately, the hack will not be ok when hosting on S3. And it prevents me to conveniently browse my file system to look at the uploaded document when debugging my iOS app.
Downloading the file directly from S3 via an FTP client such as Transmit gets me the same corrupted file.

What is the problem? Where do you think I should further troubleshoot, client or server? How? Any idea, tip, hunch of things to assert or look at?

If you don’t have any solution, I would also be glad to be given a temporary fix to set properly again the content_type (or whatever the problem is) on the corrupted file after having downloaded it.

  • 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-09T03:49:05+00:00Added an answer on June 9, 2026 at 3:49 am

    What happens if you name the uploaded file “upload.pdf” instead of “upload.pfd”?

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

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I am trying to render a haml file in a javascript response like so:

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.