我正在使用powerpoint gem 生成一個 PowerPoint,直到現在我還在使用 我是這樣創建我的PPT的: 我的PPT是這樣創建的。
現在,說說我所做的嘗試。我的第一直覺是按以下方式呼叫:
我也試過直接發送 讀取它的結果是一個比預期更大的檔案,它甚至不能夠被打開,發送 不確定如何將物件或檔案變成 uj5u.com熱心網友回復: 你需要打開二進制檔案: 這也可以通過在檔案類的實體上呼叫 不會作業,因為它在物件上呼叫了
標籤:send_file,但現在我想使用 send_data。其原因是,send_data在呼叫完成之前會阻止應用程式做其他事情,而send_file則允許應用程式在檔案下載時繼續執行,有時由于時間上的巧合,會導致檔案在用戶告訴他們的瀏覽器開始下載和網路服務器完成發送檔案之前被洗掉,從而下載一個空白或不完整的檔案。
#Creating the PowerPoint。
@deck = Powerpoint::Presentation.new
#創建一個介紹性幻燈片。
title = 'PowerPoint'/span>
副標題 = "由#{username}創建"。
@deck.add_intro title, subtitle
諸如此類的邏輯來生成幻燈片
# 保存pptx檔案。
pptname = "#{username}_powerpoint.pptx"
@deck.save(Rails.root.join('tmp', pptname))
File.open(Rails.root. join('tmp',"#{pptname}"), 'r') do |f|
send_data f.read, :filename => filename, :type => "application/vnd. ms-powerpoint", :disposition => "附件"。
結束。
@deck: send_data @deck, :fileename => pptname, : disposition => "附件", :type => "application/vnd. ms-powerpoint"
@deck的結果是一個有一張幻燈片的PowerPoint,它的標題只是。#Powerpoint::Presentation:0x64f0dd0(又稱 powerpoint 物件)。
send_data所尋找的二進制格式。path = @deck.save(Rails.root.join('tmp', pptname)
File.open(path, 'r', binmode: true) do |f|
send_data f.read, filename: 檔案名。
type: "application/vnd.ms-powerpoint"/span>。
disposition: "attachment", "附件".
結束。
#binmode方法完成。send_data @deck, ...。
to_s。因為Powerpoint::Presentation沒有實作#to_s方法,你得到的是默認的Object#to_s。
