Attach files in Rails and read them in ExtJs + CakePHP
===

I have a project that is composed of two separate parts. On one side, there is a Rails app and on the other side, there is an ExtJs client with CakePhp server.

What needs to happen is this: attach a file in Rails (this is done by using Paperclip) and be able to read them on the Cake side. It might be easy if both systems were on the same server, but this needs to be done remotely, so the Cake side will call a Rails route and Rails will provide the file.

Cake code to download file:

    function download_file($path, $file_name, $content_type, $size) {
		if ($fd = fopen ($path, "r")) {
		    header("Content-type: " . $content_type);
		    header("Content-Disposition: attachment; filename=\"".$file_name."\"");
		    header("Content-length: $size");
		    header("Cache-control: private"); //use this to open files directly
		    header('Pragma: public');
		    ob_clean();
    		flush();
		    echo readfile($path);
		    // $this->log(apache_response_headers(), 'debug');
		}
		fclose ($fd);
	}

Ruby / Rails code to provide document:

    send_file document.path, :type => document.document_content_type

Rails Document model:

    class Document < ActiveRecord::Base
        belongs_to :attachable, :polymorphic => true

        has_attached_file :document,
            :url => '/download/:id/:fingerprint/documents'

        def path
            document.path
        end
    end


Everything works as expected except one type of file, .docx. I can upload and after that download any other files except those of this type. I can read the file in rails, after it was uploaded, with no problem. There seems to be something wrong on the Cake side, as what happends there is this: I can download the file, but I can't open it with LibreOffice after that. I seems that the mime types are set up correctly as apache and the browser recognize the type of file and the application to open it. It seems I can open the file with TextEdit.

So, the question is: what is wrong? Why don't .docx files open on the php / Cakephp side?

Any ideas will be highly appreciated.
