我正在尝试使用机械化自动填写表格(包括文件上传)。我已经在GUI界面中完成了这个过程,文件上传得很好,所以我知道文件没有损坏,但当我运行mechanize脚本时,它失败了。脚本执行正确,根据调试,它上传了文件,但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并亲自查看代码/网络活动。
确保上传的文件类型正确afaik ruby mechanize指定
Content-Type: application/octet-stream
对于每种类型的文件,服务器可能都在检查ContentType,并且与所需的内容类型不正确匹配,我不记得我使用的mechanize的确切版本,但对我来说,它是在mechanize gem中硬编码的,所以你必须浏览mechanize源代码,并修复它为multipart/form数据文件元素硬编码内容类型的位置。