我有一个Android客户端希望将文件发送到我的Rails服务器。我使用Paperclip Gem用于rails,这是我在WebRick控制台中遇到的错误:
开始POST" / logs"在2015-07-23 16:51:20 +0800的192.168.63.142 ActiveRecord :: SchemaMigration Load(0.1ms)SELECT" schema_migrations"。* FROM" schema_migrations" 由LogsController处理#create as HTML 参数:{" log" => {"说明" =>"说明"," user_id" =>" 1"," file" =>#,@ original_filename =" bugreport-1hour-head.txt",@ content_type =" application / octet-stream&#34 ;,@ header =" Content-Disposition:form-data;命名= \"日志[文件] \&#34 ;; filename = \" bugreport-1hour-head.txt \" \ r \ nConContent-Type:application / octet-stream \ r \ n&n">}} Command :: file -b --mime' /tmp/9859aa87e4cbf0f33fd178012d8819b720150723-8057-lwvwns.txt' [paperclip]内容类型欺骗:文件名bugreport-1hour-head.txt(来自Headers的application / octet-stream,来自Extension的[#]),从文件命令中发现的内容类型:text / plain。请参阅文档以允许此组合。 (0.1ms)开始交易 Command :: file -b --mime' /tmp/9859aa87e4cbf0f33fd178012d8819b720150723-8057-1ndzzr1.txt' [paperclip]内容类型欺骗:文件名bugreport-1hour-head.txt(来自Headers的application / octet-stream,来自Extension的[#]),从文件命令中发现的内容类型:text / plain。请参阅文档以允许此组合。 (0.2ms)回滚事务 渲染日志/ _form.html.erb(16.6ms) 在layouts / application中呈现logs / new.html.erb(21.6ms) 在656ms完成200 OK(浏览次数:306.1ms | ActiveRecord:0.7ms)
它说
从文件命令中发现的内容类型:text / plain。请参阅文档以允许此组合。
但我已经允许在我的模型中使用内容类型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 Controller的保存行动:
# 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
答案 0 :(得分:4)
我认为此消息是通过内容欺骗的验证检查引发的。 对于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"
或强>
将其添加到初始化程序以禁用欺骗保护:
require 'paperclip/media_type_spoof_detector'
module Paperclip
class MediaTypeSpoofDetector
def spoofed?
false
end
end
end
或强>
您可以在paperclip.rb
中指定mime类型创建config/initializers
文件。
Paperclip.options[:content_type_mappings] = {
:txt=> 'application/octet-stream'
}
答案 1 :(得分:2)
您的Android客户端将文件发送为application/octet-steam
而不是text/plain
,这会导致Paperclip混淆,从而导致此异常。您可能必须修复客户端才能使其正常工作。