Я использовал Rails 5.2
и Carrierwave-qiniu
для загрузки и хранения файлов.
Когда я загружаю файлы, я не могу напрямую загрузить файл и получить ошибку:
Cannot read file http://pczd3l6pu.bkt.clouddn.com/document/129/railsguides-5.0.2.1.pdf
Но я могу просмотреть содержимое файла в Chrome с помощью URL:
http://pczd3l6pu.bkt.clouddn.com/document/129/railsguides-5.0.2.1.pdf
Все ошибки, подобные этой:
Processing by DocumentsController#download as HTML
Parameters: {"id"=>"129"}
User Load (0.3ms) SELECT `users`.* FROM `users` WHERE `users`.`id` = 2 LIMIT 1
Document Load (0.3ms) SELECT `documents`.* FROM `documents` WHERE `documents`.`id` = 129 LIMIT 1
Sent file http://pczd3l6pu.bkt.clouddn.com/document/129/railsguides-5.0.2.1.pdf (0.3ms)
Completed 500 Internal Server Error in 4ms (ActiveRecord: 0.7ms)
ActionController::MissingFile - Cannot read file http://pczd3l6pu.bkt.clouddn.com/document/129/railsguides-5.0.2.1.pdf:
app/controllers/documents_controller.rb:35:in `download'
Мой document_controllers
такой:
def create
@document = Document.new
@document.file_name = params[:file]
if @document.file_name.blank?
render json: { ok: false }, status: 400
return
end
@document.user_id = current_user.id
if @document.save
render json: { url: @document.file_name.url, id:@document.id }
else
flash[:warning] = "Upload Failed"
end
end
def destroy
@document = Document.find(params[:id])
@document.destroy
redirect_to session[:back_path].pop
flash[:success] = 'Please upload files again'
end
def download
@document = Document.find(params[:id])
send_file(@document.file_name.url, disposition: 'attachment')
end
Мой migration
документов:
def change
create_table :documents do |t|
t.string :file_name
t.string :file_type
t.integer :user_id
t.timestamps
end
add_index :documents, [:user_id, :created_at]
end
Мой routes.rb
такой:
resources :documents, only: [:create,:destroy] do
member do
get :download
end
end
Как можноЯ прямо скачиваю файлы?Большое спасибо.