使用rails获取本地html文件的内容

发布于 2024-10-21 22:55:52 字数 325 浏览 5 评论 0原文

我需要的是将本地 html 文件传递​​到我的表单,获取该文件的内容,然后解析它。 我看到了这个

  = form_tag :parser, :html => {:multipart => true} do
    = file_field_tag :html_file
    = submit_tag

并且在控制器中这个

  def parser
    @file = params[:html_file]
  end

似乎表单没有获取文件,只是获取带有文件名的字符串。如何修复它?

what i need is passing local html file to my form, get content of this file and later parse it.
I have this in view

  = form_tag :parser, :html => {:multipart => true} do
    = file_field_tag :html_file
    = submit_tag

And this in controller

  def parser
    @file = params[:html_file]
  end

It seems like form dont get file, just get the string with name of file. How to fix it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的痛♀有谁懂 2024-10-28 22:55:52

我写这个是为了处理 Rails 2.3.x 应用程序中的上传。我不记得为什么我必须将其拆分为 if/elsif/else/end 语句,但我一定在将其合并为一个 if 语句时犯了一个错误。

    if %w(File Tempfile ActionController::UploadedTempfile ).include?(params[:html_file].class.to_s)          
      data = params[:html_file].read
    elsif %w(StringIO ActionController::UploadedStringIO).include?(params[:html_file].class.to_s)
      data = params[:html_file].read
    else
      logger.error("File does not appear to be a valid class.")
    end

I wrote this to handle uploads in a Rails 2.3.x app. I cannot remember why I had to split this into an if/elsif/else/end statement, but I must have had an error consolidating it to one if statement.

    if %w(File Tempfile ActionController::UploadedTempfile ).include?(params[:html_file].class.to_s)          
      data = params[:html_file].read
    elsif %w(StringIO ActionController::UploadedStringIO).include?(params[:html_file].class.to_s)
      data = params[:html_file].read
    else
      logger.error("File does not appear to be a valid class.")
    end
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文