【问题标题】:Rails Errno::ENOENT: No such file or directory when saving an uploadRails Errno::ENOENT:保存上传时没有这样的文件或目录
【发布时间】:2015-12-14 20:50:10
【问题描述】:

我的 Rails API 遇到以下问题。

我正在尝试将文件保存到临时目录中。我最初的假设是我应该能够将它保存到系统/tmp 目录中,因为这就是该目录的用途。所以我的gallery_images_controller.rb 中有以下代码:

    def upload
        # set the upload
        upload = params[:qqfile].original_filename
        # we want to save files in tmp for now
        directory = "/tmp"
        ap(directory)
        # create the full path for where to save the file
        path = File.join(directory, upload)
        ap(path)
        # get the md5 of the file so we can use it as the key in the json response
        md5 = Digest::MD5.file(path).hexdigest
        # save the file
        File.open(path, "w+b") { |f| f.write(params[:qqfile].read) }
        # render the result with the md5 as the key and the filename and full path
        render json: {success: true, upload: {"#{md5}": {filename: upload, full_path: path}}}, status: 200
    end

当我发送带有文件的发布请求时,我收到以下错误:

{:error=>true, :message=>"Errno::ENOENT: No such file or directory @ rb_sysopen - /tmp/3Form-Museum-of-Science-25.jpg"}

我也尝试将文件保存在 Rails.root tmp 文件夹中并得到相同的错误:

directory = "#{Rails.root}/tmp/"

{:error=>true, :message=>"Errno::ENOENT: No such file or directory @ rb_sysopen - /vagrant/tmp/3Form-Museum-of-Science-25.jpg"}

我也试过w+bwb的模式,没用。

Ruby (http://ruby-doc.org/core-2.2.3/IO.html#method-c-new) 的文档说,如果文件不存在,ww+ 模式应该创建文件。这正是我想要它做的事情,但事实并非如此。

另外,我已经检查了文件夹的权限。如您所料,/tmp 文件夹有 777,而 rails 根目录 /vagrant/tmp 有 755,就像 rails 根目录中的所有其他文件夹一样。

请帮忙!


系统信息:

  • 开发:运行 ubuntu 14.04、unicorn 和 nginx 的 Vagrant box
  • 产品:运行 ubuntu 14.04、unicorn 和 nginx 的 Amazon EC2

【问题讨论】:

    标签: ruby-on-rails ruby ruby-on-rails-3 nginx amazon-ec2


    【解决方案1】:

    你应该只运行

    File.open(path, "w+b") { |f| f.write(params[:qqfile].read) }
    

    之前

    md5 = Digest::MD5.file(path).hexdigest
    

    只需交换两行,以便在计算十六进制摘要之前创建文件

    【讨论】:

    • 谢谢,一位朋友也帮我解决了这个问题。我发布了我的答案,就像我刷新了页面并看到了你的一样,所以我接受了你的答案和答案!再次感谢!
    【解决方案2】:

    对于任何在这里遇到同样问题的人来说,这就是我为解决它所做的。

    为了解决这个问题,我修复了 2 个问题:

    1. 我改变了这一行:

      File.open(路径, "w+b") { |f| f.write(params[:qqfile].read) }

    到这里:

    File.open(path, "w+b") { |f| f.write(path) }
    
    1. 将上面的行移到 md5

    所以现在我的上传功能是这样的:

    def upload
            # set the upload
            upload = params[:qqfile].original_filename
            # we want to save files in tmp for now
            directory = "/tmp"
            # create the full path for where to save the file
            path = File.join(directory, upload)
            # save the file
            File.open(path, "w+b") { |f| f.write(path) }
            # get the md5 of the file so we can use it as the key in the json response
            md5 = Digest::MD5.file(path).hexdigest
            # render the result with the md5 as the key and the filename and full path
            render json: {success: true, upload: {"#{md5}": {filename: upload, full_path: path}}}, status: 200
    end
    

    【讨论】:

      猜你喜欢
      • 2014-10-23
      • 1970-01-01
      • 1970-01-01
      • 2020-02-22
      • 2015-06-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多