将文本文件从 Android 上传到 Rails 会出现内容类型欺骗错误
uploading text file from Android to Rails gives Content Type Spoof Error
我有一个 Android 客户端希望将文件发送到我的 Rails 服务器。我正在为 rails 使用 Paperclip Gem,这是我在 WebRick 控制台中遇到的错误:
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)
它说
content type discovered from file command: text/plain. See documentation to allow this combination.
但我已经在下面的模型中允许内容类型 text/plain
Android代码:
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 具有回形针值的模型:
class Log < ActiveRecord::Base
has_attached_file :file
validates_attachment_content_type :file, :content_type => "text/plain"
结束
Rails 控制器的保存操作:
# 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
我认为此消息是由内容欺骗的验证检查引发的。
对于 Paperclip v.4 这会产生一个错误 [https://github.com/thoughtbot/paperclip/issues/1429][1]
虽然对于 Paperclip v.3,它似乎只是抛出弃用警告,[https://github.com/thoughtbot/paperclip/issues/1423][2]
所以要么..在Gemfile
中使用它
gem "paperclip", "~> 3.5.3"
或
将此添加到初始化程序以禁用欺骗保护:
config/initializers/paperclip_media_type_spoof_detector_override.rb
require 'paperclip/media_type_spoof_detector'
module Paperclip
class MediaTypeSpoofDetector
def spoofed?
false
end
end
end
或
您可以指定 mime 类型在 config/initializers
中创建了一个 paperclip.rb
文件。
Paperclip.options[:content_type_mappings] = {
:txt=> 'application/octet-stream'
}
您的 Android 客户端将文件作为 application/octet-steam
而不是 text/plain
发送,这会导致 Paperclip 混淆,从而导致此异常。您可能必须修复客户端才能使其正常工作。
我有一个 Android 客户端希望将文件发送到我的 Rails 服务器。我正在为 rails 使用 Paperclip Gem,这是我在 WebRick 控制台中遇到的错误:
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)
它说
content type discovered from file command: text/plain. See documentation to allow this combination.
但我已经在下面的模型中允许内容类型 text/plain
Android代码:
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 具有回形针值的模型:
class Log < ActiveRecord::Base
has_attached_file :file
validates_attachment_content_type :file, :content_type => "text/plain"
结束
Rails 控制器的保存操作:
# 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
我认为此消息是由内容欺骗的验证检查引发的。 对于 Paperclip v.4 这会产生一个错误 [https://github.com/thoughtbot/paperclip/issues/1429][1]
虽然对于 Paperclip v.3,它似乎只是抛出弃用警告,[https://github.com/thoughtbot/paperclip/issues/1423][2]
所以要么..在Gemfile
gem "paperclip", "~> 3.5.3"
或
将此添加到初始化程序以禁用欺骗保护:
config/initializers/paperclip_media_type_spoof_detector_override.rb
require 'paperclip/media_type_spoof_detector'
module Paperclip
class MediaTypeSpoofDetector
def spoofed?
false
end
end
end
或
您可以指定 mime 类型在 config/initializers
中创建了一个 paperclip.rb
文件。
Paperclip.options[:content_type_mappings] = {
:txt=> 'application/octet-stream'
}
您的 Android 客户端将文件作为 application/octet-steam
而不是 text/plain
发送,这会导致 Paperclip 混淆,从而导致此异常。您可能必须修复客户端才能使其正常工作。