当看到“Getting Errno::ENOENT”时,如何在 Rails 中获取正确的 Tempfile 文件路径没有这样的文件或目录@rb_sysopen“”
我想在我的网站上显示演示文稿文件的预览。我正在尝试创建一个临时文件,该文件从存储在活动存储中的 Microsoft PowerPoint Open XML (.pptx
) 文件中读取。
我在临时文件上使用 Docsplit.extract_images 来将幻灯片转换为图像,以某种形式的图像轮播将其显示为预览。
我有幻灯片参数,例如 [:name, :ppt,pages: [] ]
,其中 ppt
是 has_one_attached
,pages 是 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 to string 错误。
I want to show a preview of presentation files on my website. I am trying to make a tempfile which reads from a Microsoft PowerPoint Open XML (.pptx
) file stored in active storage.
I am using Docsplit.extract_images
on the tempfile to convert the slides to images to show it as a preview in some form of image carousel.
I have slide parameterss such as [:name, :ppt, pages: [] ]
where ppt
is has_one_attached
and pages is has_many_attached
. This is what my slides_controller look like:
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
I am getting a Errno::ENOENT no such file or directory @ rb_sysopen
error.
Is this the correct way to get the tempfile
path?
Also, If I use tempfile.path
instead of "#{tempfile.path}"
, I get a nil to string error.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确保您正在使用 tempfile.close!这将取消链接文件 https://ruby-doc.org/stdlib-2.5.3/libdoc/tempfile/rdoc/Tempfile.html#method-i-close-21
如果你只是使用 close 而没有一声巨响(!)那么你应该已经准备好了!
In your ensure you are using tempfile.close! this unlinks the file per https://ruby-doc.org/stdlib-2.5.3/libdoc/tempfile/rdoc/Tempfile.html#method-i-close-21
If you just use close without the bang (!) then you should be all set!