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:

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.
What happens if you name the uploaded file “upload.pdf” instead of “upload.pfd”?