0

我想在我的网站上显示演示文件的预览。我正在尝试制作一个临时文件,该文件从.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 到字符串错误。

4

1 回答 1

0

确保您使用的是 tempfile.close!这会根据https://ruby-doc.org/stdlib-2.5.3/libdoc/tempfile/rdoc/Tempfile.html#method-i-close-21取消链接文件

如果您只使用 close 而没有 bang (!) 那么您应该一切就绪!

于 2022-03-05T17:30:19.810 回答