Sinatra send_file 在尝试存储文件时出现 404 错误

Sinatra send_file gives 404 error when trying to store the file

我正在尝试使用 postman 模拟下载文件

回应 127.0.0.1 - - [18/Jan/2021:03:54:47 -0800] "POST / HTTP/1.1" 404 - 4.4450

命令 send_file "#{filename}", :disposition=> "attachment", :filename => filename, :type => 'application/octet-stream'

post '/' do
  fileSize = env['CONTENT_LENGTH'].to_i/1048576.0 # Converting to MB
  if params.has_key?(:file) && fileSize <= 1
    filename = params['file']["filename"]
    send_file "#{filename}", :disposition=> "attachment", :filename => filename, :type => 'application/octet-stream'
    puts ".......................FILE STORED........................."
  else
    puts "....................FILE NOT VALID........................."
  end  
end

看到您对我的评论的回复后,我可以说这不起作用的原因是 send_file 命令没有提供正确的参数。根据这些文档:https://apidock.com/rails/v2.3.8/ActionController/Streaming/send_file - 您必须将文件路径附加为第一个参数,而不仅仅是文件名。

此外,文件的位置(您的下载文件夹)可能会导致其他问题。该文件应该可以从项目目录中的某处获得,最好在 /public 文件夹中。

总结一下:

  • 将文件移动到项目中的 /public 目录。
  • 在文件名前加上文件存储路径。