Program Tip

레일의 파일 다운로드 링크

programtip 2020. 11. 29. 12:13
반응형

레일의 파일 다운로드 링크


방문자에게 pdf를 다운로드 할 수있는 옵션을 제공하고 싶습니다. 나는 시도했다 :

<%= link_to "abc", "/data/abc.pdf"%>

<%= link_to "abc", "/data/abc.pdf", :format => 'pdf' %>

몇 가지 변형이 있지만 작동하지 않는 것 같습니다. 나는 계속No route matches [GET] "/data/abc.pdf"

자산 폴더에있는 data라는 폴더에 pdf 파일이 있습니다. 어떤 도움을 주시면 감사하겠습니다.


레일스 4 :

경로 :

get "home/download_pdf"

컨트롤러에서 (이미 pdf 있음) :

def download_pdf
  send_file(
    "#{Rails.root}/public/your_file.pdf",
    filename: "your_custom_file_name.pdf",
    type: "application/pdf"
  )
end

컨트롤러에서 (pdf 생성 필요) :

require "prawn"
class ClientsController < ApplicationController

  def download_pdf
    client = Client.find(params[:id])
    send_data generate_pdf(client),
              filename: "#{client.name}.pdf",
              type: "application/pdf"
  end

  private

  def generate_pdf(client)
    Prawn::Document.new do
      text client.name, align: :center
      text "Address: #{client.address}"
      text "Email: #{client.email}"
    end.render
  end
end

보기 :

<%= link_to 'Download PDF', home_download_pdf_url %>

레일즈 3

그것을하는 방법 :

def download
  send_data pdf,
    :filename => "abc.pdf",
    :type => "application/pdf"
end

이 대안으로 가야합니다.

레일 <3

공용 폴더의 파일

이것이 답이 될 수 있습니다 : Rails 애플리케이션에서 파일을 다운로드하는 방법

파일을 공용 폴더에 넣어야합니다. 이것이 트릭입니다.

파일이 올바르게 배치되면 작동합니다.

파일을 공용 폴더로 이동할 수없는 경우 알려주십시오.

컨트롤러를 통해 다운로드

downlaod 액션과 컨트롤러를 만들고 link_to그것을

  def download
    send_file '/assets/data/abc.pdf', :type=>"application/pdf", :x_sendfile=>true
  end

레일스 4 :

경로 :

get "home/download_pdf"

컨트롤러에서 (이미 pdf 있음) :

def download_pdf
  send_file(
    "#{Rails.root}/public/your_file.pdf",
    filename: "your_custom_file_name.pdf",
    type: "application/pdf"
  )
end

컨트롤러에서 (pdf 생성 필요) :

require "prawn"
class ClientsController < ApplicationController

  def download_pdf
    client = Client.find(params[:id])
    send_data generate_pdf(client),
              filename: "#{client.name}.pdf",
              type: "application/pdf"
  end

  private

  def generate_pdf(client)
    Prawn::Document.new do
      text client.name, align: :center
      text "Address: #{client.address}"
      text "Email: #{client.email}"
    end.render
  end
end

보기 :

<%= link_to 'Download PDF', home_download_pdf_url %>

If the files are static (meaning they don't change), place them in the public folder.

Then you can download like

<a href="file.pdf" download>PDF</a>

or with ERB

<%= link_to 'PDF', 'file.pdf', download: '' %>

and to give the file another name for downloading, just pass that name to the download option

<%= link_to 'PDF', 'file.pdf', download: 'data' %>

This will download the file as data.pdf instead of file.pdf.


you can simply call your controller action like this

<%= link_to "Download", download_file_path, class: "btn btn-sm btn-default", target: "_blank" %>

and in your controller

def download_file
 redirect_to paperclip_attachment.file.url
end

I Struggle a lot to find simple way to Auto Downlaod Some File From Public Directory. Finally i come up with this solution. For Example: i Have my file in SVG folder inside Public Directory.

Public/svg/Test1.xlsx

Now When i try to Access it load it and Give Path with Paper clip it give issue. Even When i try full path it give issue as well so we can make it dynamic path. First Get Path of the Host so that Can Redirect Easily. <% url = request.original_url.chomp(request.fullpath) %>

Now we Can Access any file in Public Folder Like below and Pass id and Download option. Download option rename any file which u want to download.

<%= link_to 'Database File', "#{url}/svgs/Test1.xlsx", download: 'Data.xlsx',id: "Link_to_Downlaod" %>

Now Click able link is Ready We Can Click on Above link to Download the File. Use the Following Script to Auto Download the File.

  <script type="text/javascript">
    window.onload = document.getElementById('Link_to_Downlaod').click();
  </script>
</div>

For the Case of PDF or any Any other file type just need to change the file extension.

참고URL : https://stackoverflow.com/questions/13164063/file-download-link-in-rails

반응형