I have an Android client that hopes to send a file to my Rails server. I'm using Paperclip Gem for rails and this is the error that I'm getting at in my WebRick console:
Started POST "/logs" for 192.168.63.142 at 2015-07-23 16:51:20 +0800 ActiveRecord::SchemaMigration Load (0.1ms) SELECT "schema_migrations".* FROM "schema_migrations" Processing by LogsController#create as HTML Parameters: {"log"=>{"description"=>"Description", "user_id"=>"1", "file"=>#, @original_filename="bugreport-1hour-head.txt", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"log[file]\"; filename=\"bugreport-1hour-head.txt\"\r\nContent-Type: application/octet-stream\r\n">}} Command :: file -b --mime '/tmp/9859aa87e4cbf0f33fd178012d8819b720150723-8057-lwvwns.txt' [paperclip] Content Type Spoof: Filename bugreport-1hour-head.txt (application/octet-stream from Headers, [#] from Extension), content type discovered from file command: text/plain. See documentation to allow this combination. (0.1ms) begin transaction Command :: file -b --mime '/tmp/9859aa87e4cbf0f33fd178012d8819b720150723-8057-1ndzzr1.txt' [paperclip] Content Type Spoof: Filename bugreport-1hour-head.txt (application/octet-stream from Headers, [#] from Extension), content type discovered from file command: text/plain. See documentation to allow this combination. (0.2ms) rollback transaction Rendered logs/_form.html.erb (16.6ms) Rendered logs/new.html.erb within layouts/application (21.6ms) Completed 200 OK in 656ms (Views: 306.1ms | ActiveRecord: 0.7ms)
It says
content type discovered from file command: text/plain. See documentation to allow this combination.
But I already allowed content-type text/plain in my model below
Android code:
mSendLogs.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String url = "http://192.168.63.145:3000/logs";
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(),
"bugreport-1hour-head.txt");
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(url);
MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
multipartEntity.addPart("log[description]", new StringBody("Description"));
multipartEntity.addPart("log[user_id]", new StringBody("1"));
multipartEntity.addPart("log[file]", new FileBody(file) );
httppost.setEntity(multipartEntity);
HttpResponse response = httpclient.execute(httppost);
String statusCode = response.getEntity().getContent().toString();
Log.d("Benggal", "http.fr.server: " + statusCode + "Upload Logs");
} catch (Exception e) {
Log.e("Benggal",e.toString());
}
}
});
Rails Model With Paperclip Values:
class Log < ActiveRecord::Base
has_attached_file :file
validates_attachment_content_type :file, :content_type => "text/plain"
end
Rails Controller's Action For Saving:
# POST /logs
# POST /logs.json
def create
@log = Log.new(log_params)
respond_to do |format|
if @log.save
format.html { redirect_to @log, notice: 'Log was successfully created.' }
format.text {@log.file.url}
format.json { render :show, status: :created, location: @log }
else
format.html { render :new }
format.json { render json: @log.errors, status: :unprocessable_entity }
end
end
end
application/octet-stream
, nottext/plain
and that mismatch causes an exception. – katafrakt