If you use Rails on the backend, you can hit the URL to download the file.
I'm assuming you're uploading a file with Active Storage. ** Save destination (using S3 or minio) **
This time, set the routing so that it is **/upload_files /: id/download **.
resources :upload_files do
member do
get :download
end
end
def download
upload_file = UploadFile.find(params[:id])
file = upload_file.file.blob.download #to download
if send_data(file, disposition: 'attachment', #Send the downloaded file
filename: upload_file.file.blob.filename.to_s, #Get file name
type: upload_file.file.blob.content_type) # content_Get type
head :no_content #If you can send, no_Returns content
else
render json: upload_file.errors, status: :not_found #Returns an error
end
end
You can actually download the file from Active Storage by setting file = upload_file.file.blob.download
.
I was having a hard time finding the above method, but it was written in the official ActiveStorage documentation! ** ** If you are in trouble, the formula is the best. https://railsguides.jp/active_storage_overview.html
In addition, the file name is dynamically acquired by setting upload_file.file.blob.filename.to_s
.
This will cause the ** filename to correctly include the extension. ** **
I realized that it is important to read the formula if you are in trouble!
Recommended Posts