可以将file_field设置为多个文件?

时间:2014-03-24 19:37:51

标签: ruby watir watir-webdriver

我有一个带有以下HTML的文件上传输入:

<input type="file" id="assets" name="assets" multiple="multiple" webkitdirectory="webkitdirectory">

我可以上传一个文件:

single_file = "/Users/me/Downloads/Demo Folder/1428441/1.wav"
upload = @browser.file_field(:id => 'assets')
upload.set single_file

然而,当我尝试上传多个文件时,通过选择顶级目录(如果我尝试在我的浏览器中手动执行此操作,则可以正常工作):

assets_folder = "/Users/me/Downloads/Demo Folder/1428441"
puts "Does dir exist? #{File.directory?(assets_folder)}" # true

upload = @browser.file_field(:id => 'assets')
upload.set assets_folder

根本无法上传。如果我尝试创建一个包含2个不同文件的文件路径的数组:

files = []
files.push("/Users/me/Downloads/Demo Folder/1428441/1.wav")
files.push("/Users/me/Downloads/Demo Folder/1428441/2.wav")
upload = @browser.file_field(:id => 'assets')
upload.set files

我发现此方法只接受字符串作为参数:can't convert Array into String (TypeError)

是否可以通过file_field的{​​{1}}方法选择多个文件?

如果没有,我还可以尝试其他解决方法吗?我简要地看了一下尝试注入JavaScript,但显然这也不可能,根据“How to set a value to a file input in HTML?

1 个答案:

答案 0 :(得分:0)

尝试设置多个文件路径,用分号分隔它们,看看是否有效(未经测试),如下所示:

multiple_file = ("/Users/me/Downloads/Demo Folder/1428441/1.wav" ; "/Users/me/Downloads/Demo Folder/1428441/some_else.wav")