我想在我的網站上顯示演示檔案的預覽。我正在嘗試制作一個臨時檔案,該檔案從.pptx存盤在活動存盤中的 Microsoft PowerPoint Open XML ( ) 檔案中讀取。
我正在使用Docsplit.extract_images臨時檔案將幻燈片轉換為影像,以某種形式的影像輪播將其顯示為預覽。
我有幻燈片引數,例如[:name, :ppt, pages: [] ]where pptishas_one_attached和 pages is has_many_attached。這是我的 slides_controller 的樣子:
def create
@slide = Slide.new(slide_params)
respond_to do |format|
if @slide.save
tempfile = Tempfile.new([ 'foobar', '.pptx'])
tempfile.binmode
begin
@slide.ppt.download { |chunk| tempfile.write(chunk) }
tempfile.flush
tempfile.rewind
ensure
tempfile.close!
end
@slide.pages << Docsplit.extract_images("#{tempfile.path}", :size => '1080x', :format => [:png])
tempfile.unlink
format.html { redirect_to slide_url(@slide), notice: "Slide was successfully created." }
format.json { render :show, status: :created, location: @slide }
else
format.html { render :new, status: :unprocessable_entity }
format.json { render json: @slide.errors, status: :unprocessable_entity }
end
end
end
我收到一個Errno::ENOENT no such file or directory @ rb_sysopen錯誤。
這是獲取tempfile路徑的正確方法嗎?
此外,如果我使用tempfile.path而不是"#{tempfile.path}",我會得到一個 nil 到字串錯誤。
uj5u.com熱心網友回復:
確保您使用的是 tempfile.close!這會根據https://ruby-doc.org/stdlib-2.5.3/libdoc/tempfile/rdoc/Tempfile.html#method-i-close-21取消鏈接檔案
如果您只使用 close 而沒有 bang (!) 那么您應該一切就緒!
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/440722.html
