我正在尝试使用Mechanize自动填写表单(包括文件上传)。我已经在GUI界面中完成了这个过程并且文件上传很好,所以我知道文件没有损坏,但是当我运行我的机械化脚本时它失败了。该脚本正确执行,并根据调试它上传文件,但Canvas(我上传到的服务)说文件无法读取。我已经联系过Canvas支持但他们无法提供帮助,因为它是非标准使用他们的系统。
这是脚本(已匿名化):
09 mech = Mechanize.new
10 mech.log = Logger.new(STDOUT)
11 mech.user_agent_alias = 'Mac Mozilla'
12 mech.get("https://ucdenver.test.instructure.com") do |page|
13 page.form_with(:action => "/login") do |f|
14 user_field = f.field_with(:name => "pseudonym_session[unique_id]")
15 user_field.value = user
16 pwd_field = f.field_with(:name => "pseudonym_session[password]")
17 pwd_field.value = pwd
18 end.submit
19 end
20
21 mech.get("https://ucdenver.test.instructure.com/accounts/1") do |page|
22 page.form_with(:action => "/accounts/1/courses") do |f|
23 course_field = f.field_with(:name => "course[name]")
24 course_field.value = "38492"
25 end.submit
26 end
27
28 mech.page.link_with(:href => %r/settings/,
29 :class => "settings").click
30
31 mech.page.link_with(:href => %r/import/,
32 :text => %r/Import Content/).click
33
34 mech.page.link_with(:href => %r/imports\/migrate/,
35 :text => %r/Import content from a content package/).click
36
37 mech.page.form_with(:action => %r/imports\/migrate/) do |f|
38 export_system = "migration_settings[migration_type]"
39 f[export_system] = "blackboard_exporter"
40 f.file_uploads.first.mime_type = "application/zip"
41 f.file_uploads.first.file_name = bkb_export
42 end.submit
Canvas支持称服务器正在返回:
Could not unzip archive file, exit status 9, message:
[/mnt/var/web/migration_tool/data/attachment_420130214-13303-sv6ue5-0.jpg] End-of-central-
directory signature not found. Either this file is not a zipfile, or it constitutes one
disk of a multi-part archive. In the latter case the central directory and zipfile comment
will be found on the last disk(s) of this archive. unzip: cannot find zipfile directory in
one of /mnt/var/web/migration_tool/data/attachment_420130214-13303-sv6ue5-0.jpg or
/mnt/var/web/migration_tool/data/attachment_420130214-13303-sv6ue5-0.jpg.zip, and cannot
find /mnt/var/web/migration_tool/data/attachment_420130214-13303-sv6ue5-0.jpg.ZIP, period.
不幸的是,这个错误对我来说毫无意义,但是当脚本上传.zip文件时,它引用了几个.jpg似乎很奇怪。任何想法或帮助将不胜感激,如果我留下任何有用的东西,我很乐意提供更多信息。
如果您真的感觉像是一个吸气者,可以在http://canvas.instructure.com注册一个免费帐户,并亲自查看代码/网络活动。
答案 0 :(得分:0)
确保您使用正确的类型上传文件afaik ruby-mechanize指定
Content-Type: application/octet-stream
对于每种类型的文件,很可能服务器正在检查ContentType并且没有正确匹配所需的内容类型,我不记得我使用的机械化的确切版本,但对我来说,它是在机械化宝石中硬编码所以你有浏览机械化的源代码并修复multipart / form-data文件元素的硬编码内容类型。