Mayuresh Anand
Mayuresh Anand

Reputation: 243

Sinatra send_file gives 404 error when trying to store the file

I am trying to download the file by simulating using postman

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

Command 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

Upvotes: 0

Views: 240

Answers (1)

James Webb
James Webb

Reputation: 189

After seeing your replys to my comments I can say the reason why this is not working is because the send_file command is not being supplied with the correct argments. According to these docs: https://apidock.com/rails/v2.3.8/ActionController/Streaming/send_file - you must attach the file path as the first parameter and not just the file name.

Also, the location of the file (your downloads folder) may cause other problems. The file should be available from within your project directory somewhere, preferably in the /public folder.

To summarise:

  • Move the file to the /public directory within your project.
  • Prepend the file name with the path to where the file will be stored.

Upvotes: 1

Related Questions