首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Mechanize上传文件失败

使用Mechanize上传文件失败
EN

Stack Overflow用户
提问于 2013-02-19 03:00:52
回答 1查看 479关注 0票数 0

我正在尝试自动填写一个表单(其中包括一个文件上传)使用机械。我已经在GUI界面中完成了这个过程,文件上传得很好,所以我知道文件没有损坏,但是当我运行我的机械化脚本时,它失败了。脚本执行正确,根据调试,它上传了文件,但Canvas (我上传到的服务)显示无法读取该文件。我已经联系了Canvas支持人员,但他们无法提供帮助,因为这是对他们系统的非标准使用。

以下是脚本(已被匿名):

代码语言:javascript
复制
 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支持人员说服务器正在返回:

代码语言:javascript
复制
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文件,这似乎很奇怪。任何想法或帮助都会非常感谢,如果我遗漏了任何有用的东西,我很乐意提供更多的信息。

如果你真的觉得自己是一个go getter,你可以在http://canvas.instructure.com注册一个免费帐户,自己看看代码/网络活动。

EN

回答 1

Stack Overflow用户

发布于 2014-09-11 20:56:10

确保您上传的文件具有正确的类型afaik ruby-mechanize指定

代码语言:javascript
复制
Content-Type: application/octet-stream

对于每种类型的文件,很可能服务器正在检查ContentType,并且没有正确地匹配所需的内容类型,我不记得我使用的mechanize的确切版本,但对我来说,它是硬编码在mechanize gem中的,所以你必须通过mechanize的源代码并修复它是多部分/形式数据文件元素的硬编码内容类型。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/14943482

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档