简体   繁体   中英

Accessing filename through a form

I'm trying to get the filename of an uploaded file through a form. params returns data like so:

(byebug) pp params
{"enable_test"=>"1",
 "tokens_per_day"=>"0",
 "unlimited_tokens"=>"0",
 "test_scripts_attributes"=>
  {"0"=>
    {"seq_num"=>"1.0",
     "script_name"=>
      #<ActionDispatch::Http::UploadedFile:0x00000007b370c0
       @content_type="application/pdf",
       @headers=
        "Content-Disposition: form-data; name=\"assignment[test_scripts_attributes][0][script_name]\"; filename=\"ass5import.pdf\"\r\nContent-Type: application/pdf\r\n",
       @original_filename="ass5import.pdf",
       @tempfile=#<File:/tmp/RackMultipart20151115-5586-patihn.pdf>>,
     "description"=>"",
     "max_marks"=>"2",
     "run_on_submission"=>"0",
     "run_on_request"=>"0",
     "halts_testing"=>"0",
     "display_description"=>"display_after_submission",
     "display_run_status"=>"display_after_submission",
     "display_marks_earned"=>"display_after_submission",
     "display_input"=>"display_after_submission",
     "display_expected_output"=>"display_after_submission",
     "display_actual_output"=>"display_after_submission"}}}
{"enable_test"=>"1", "tokens_per_day"=>"0", "unlimited_tokens"=>"0", "test_scripts_attributes"=>{"0"=>{"seq_num"=>"1.0", "script_name"=>#<ActionDispatch::Http::UploadedFile:0x00000007b370c0 @tempfile=#<Tempfile:/tmp/RackMultipart20151115-5586-patihn.pdf>, @original_filename="ass5import.pdf", @content_type="application/pdf", @headers="Content-Disposition: form-data; name=\"assignment[test_scripts_attributes][0][script_name]\"; filename=\"ass5import.pdf\"\r\nContent-Type: application/pdf\r\n">, "description"=>"", "max_marks"=>"2", "run_on_submission"=>"0", "run_on_request"=>"0", "halts_testing"=>"0", "display_description"=>"display_after_submission", "display_run_status"=>"display_after_submission", "display_marks_earned"=>"display_after_submission", "display_input"=>"display_after_submission", "display_expected_output"=>"display_after_submission", "display_actual_output"=>"display_after_submission"}}}

Right now, the code im trying to fix is storing script_name as the filename, which is wrong/ I'd like to store the filename instead. How can I access the @headers filename?

The code im trying to modify previously accessed it like so, but it seems like this is out dated and no longer works (code is from RoR 3.0)

# Filter out test support files that need to be created and updated
    unless testsupporters.nil?
      testsupporters.each_key do |key|
        tfile = testsupporters[key]

        # Check to see if this is an update or a new file:
        # - If 'id' exists, this is an update
        # - If 'id' does not exist, this is a new test file
        tf_id = tfile['id']

        # If only the 'id' exists in the hash, other attributes were not updated
        # so we skip this entry. Otherwise, this test file possibly requires an
        # update
        if !tf_id.nil? && tfile.size > 1

          # Find existing test file to update
          @existing_testsupport = TestSupportFile.find_by_id(tf_id)
          if @existing_testsupport
            # Store test file for any possible updating
            updated_support_files[key] = tfile
          end
        end

        # Test file needs to be created since record doesn't exist yet
        if tf_id.nil? && tfile['file_name']
          updated_support_files[key] = tfile
        end
      end
    end

The code you posted doesn't reference params anywhere, and there's no way to know what kind of object techsupporters is. So I can't tell you exactly how to modify your code.

But from the params you posted, I believe that what you want is params['test_scripts_attributes']['0']['script_name'].original_filename .

In the end, the method you're looking for is ActionDispatch::Http::UploadedFile#original_filename .

From the looks of it, the params could contain anything from zero to multiple files. So be sure to account for that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM