Rails의 파일 업로드- 데이터는 객체입니다. 내 견해에서 어떻게 반환합니까?
-
05-07-2019 - |
문제
내 Rails 프로젝트에서 업로드를 할 때 데이터베이스는 저장합니다.
---! Ruby/Object : File Content_Type : Application/Octet-stream original_path : my.numbers
내 관점에서만 My.Numbers를 반환하도록하려면 어떻게해야합니까?
무리 감사! 마르코
추신. attachment_fu 또는 다른 플러그인을 사용하고 싶지 않습니다.
해결책
파일 업로드는 실제로 컨트롤러에서 데이터가 아닌 파일 객체로 수신되므로이를 읽는 것이 귀하의 책임입니다. 일반적으로 업로드 된 파일은 임시 디렉토리에 저장되며 열린 파일 핸들은 매개 변수에 있습니다.
데이터를 검색하기 위해 다음과 같은 작업을 수행 할 수 있습니다.
def create
# Read in data from file into parameter before creating anything
if (params[:model] and params[:model][:file])
params[:model][:file] = params[:model][:file].read
end
@model = MyModel.create(params[:model])
end
데이터베이스의 열이 이진 데이터를 저장할 수 있는지 확인해야 할 것입니다. MySQL 마이그레이션에서는 이것이 바이너리 열 유형입니다.
다른 팁
Helper Original_Filename으로 업로드 된 파일의 이름에 액세스 할 수 있습니다. 그래서 params [: model] [: file] .original_filename
제휴하지 않습니다 StackOverflow