Ruby: 如何以multipart/form-data格式通过HTTP上传文件?

127

我希望进行一次HTTP POST请求,看起来像是从浏览器中提交的HTML表单。具体地,需要提交一些文本字段和一个文件字段。

提交文本字段很简单,net/http的rdocs中已经有示例了,但我不知道如何同时提交文件。

Net::HTTP似乎不是最好的选择。Curb看起来不错。

14个回答

104

我喜欢RestClient。它使用 multipart 表单数据等酷炫功能封装了 net/http。

require 'rest_client'
RestClient.post('http://localhost:3000/foo', 
  :name_of_file_param => File.new('/path/to/file'))

它也支持流媒体。

gem install rest-client 可以让你开始使用。


我撤回之前的说法,文件上传现在可以工作了。我现在遇到的问题是服务器返回302,而rest-client遵循RFC(但没有浏览器这样做),并抛出异常(因为浏览器应该警告此行为)。另一个选择是使用curb,但我从未在Windows上安装成功过。 - Matt Wolfe
8
自此初始发布以来,API 有些变化,现在多部分功能的调用方式为:RestClient.post 'http://localhost:3000/foo', :upload => File.new('/path/tofile'))有关更多详细信息,请访问 http://github.com/archiloque/rest-client。 - Clinton
3
rest_client 不支持提供请求头信息。许多 REST 应用程序需要 / 预期特定类型的请求头信息,因此在这种情况下 rest_client 无法正常工作。例如,JIRA 需要一个名为 X-Atlassian-Token 的令牌。 - onknows
能否获取文件上传进度?例如已上传40%。 - Ankush
2
在编写Ruby示例时,很多人都会忽略gem install rest-clientrequire 'rest_client'这两个部分,但是它们非常重要。如果你能加上这些信息,那就太好了! - dansalmo
1
根据@onknows的评论,rest-client现在支持自定义请求头。据我所知,这已经至少从2010年4月30日发布的版本1.5.0开始实现了。 - jeffdill2

46

另一个只使用标准库的例子:

uri = URI('https://some.end.point/some/path')
request = Net::HTTP::Post.new(uri)
request['Authorization'] = 'If you need some headers'
form_data = [['photos', photo.tempfile]] # or File.open() in case of local file

request.set_form form_data, 'multipart/form-data'
response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: true) do |http| # pay attention to use_ssl if you need it
  http.request(request)
end

我尝试了很多方法,但只有这个对我有效。


4
谢谢。有一个小细节需要更正,第一行应该是:uri = URI('https://some.end.point/some/path')这样你后面调用uri.porturi.host就不会出错了。 - davidkovsky
2
如果不是临时文件,而你想从你的磁盘上传文件,应该使用 File.open 而不是 File.read - Anil Yanduri
2
大多数情况下需要一个文件名,这是我添加的形式: form_data = [['file',File.read(file_name),{filename: file_name}]] - ZsJoska
9
这是正确的答案。人们应该尽可能停止使用外壳包装(gem),回归基础知识。 - Carlos Roque
1
终于找到了一些真正可用的代码!!!谢谢 - Brad
显示剩余2条评论

41

关于Nick Sieger的multipart-post库,我无法说够好的话。

它直接添加了对Net::HTTP的多部分发布支持,免去了您手动处理边界或使用与您目标不同的大型库的需要。

以下是如何使用它的简单示例,可参考README

require 'net/http/post/multipart'

url = URI.parse('http://www.example.com/upload')
File.open("./image.jpg") do |jpg|
  req = Net::HTTP::Post::Multipart.new url.path,
    "file" => UploadIO.new(jpg, "image/jpeg", "image.jpg")
  res = Net::HTTP.start(url.host, url.port) do |http|
    http.request(req)
  end
end

你可以在这里检查库: http://github.com/nicksieger/multipart-post

或者使用以下安装它:

$ sudo gem install multipart-post

如果你通过SSL连接,你需要像这样开始连接:

n = Net::HTTP.new(url.host, url.port) 
n.use_ssl = true
# for debugging dev server
#n.verify_mode = OpenSSL::SSL::VERIFY_NONE
res = n.start do |http|

3
这对我来说就是所需之物,正是我在寻找的,也正是应该包含的,而不需要宝石。Ruby 在某些方面领先,但在另一些方面却落后。 - Trey
太棒了,这简直就是天赐之物!我用它来猴子补丁OAuth gem,以支持文件上传。只花了我5分钟。 - mxk
@matthias 我正在尝试使用OAuth gem上传照片,但失败了。你能给我一些你的monkeypatch示例吗? - Hooopo
1
这个补丁非常特定于我的脚本(快速而肮脏),但是看一下它,也许你可以想出一个更通用的方法(https://gist.github.com/974084)。 - mxk
3
多部分不支持请求头。所以如果你例如想使用JIRA REST接口,多部分只会浪费宝贵的时间。 - onknows
实际上,从版本'1.1.3 / 2011-07-25'开始支持头文件。像这样初始化一个多部分类型对象:Net :: HTTP :: Post :: Multipart.new(url,{filename => file},{'custom' =>'header'})请注意,文件哈希是第二个参数,标题哈希是第三个参数。有关multipartable.rb的详细信息,请参阅源代码。 - alexanderbird

31

curb似乎是一个很好的解决方案,但如果它不能满足您的需求,您可以使用Net::HTTP。多部分表单提交只是一个格式良好的字符串,带有一些额外的头部信息。似乎每个需要进行多部分提交的Ruby程序员最终都会编写自己的小型库,这让我想知道为什么这种功能没有内置在Ruby中。也许是有的......总之,为了让您愉快地阅读,我将在此提供我的解决方案。这段代码基于我在几个博客中发现的示例,但我很遗憾无法再找到链接了。所以我想我只能将所有功劳都归于自己...

我为此编写的模块包含一个公共类,用于从StringFile对象的哈希生成表单数据和头部信息。例如,如果您想要发布一个名为"title"的字符串参数和一个名为"document"的文件参数的表单,则应该执行以下操作:

#prepare the query
data, headers = Multipart::Post.prepare_query("title" => my_string, "document" => my_file)

然后您只需使用Net::HTTP进行普通的POST

http = Net::HTTP.new(upload_uri.host, upload_uri.port)
res = http.start {|con| con.post(upload_uri.path, data, headers) }

或者以其他方式进行 POST。重点是,Multipart 返回您需要发送的数据和标头。就是这样!很简单,对吧?下面是 Multipart 模块的代码(您需要 mime-types gem):

# Takes a hash of string and file parameters and returns a string of text
# formatted to be sent as a multipart form post.
#
# Author:: Cody Brimhall <mailto:brimhall@somuchwit.com>
# Created:: 22 Feb 2008
# License:: Distributed under the terms of the WTFPL (http://www.wtfpl.net/txt/copying/)

require 'rubygems'
require 'mime/types'
require 'cgi'


module Multipart
  VERSION = "1.0.0"

  # Formats a given hash as a multipart form post
  # If a hash value responds to :string or :read messages, then it is
  # interpreted as a file and processed accordingly; otherwise, it is assumed
  # to be a string
  class Post
    # We have to pretend we're a web browser...
    USERAGENT = "Mozilla/5.0 (Macintosh; U; PPC Mac OS X; en-us) AppleWebKit/523.10.6 (KHTML, like Gecko) Version/3.0.4 Safari/523.10.6"
    BOUNDARY = "0123456789ABLEWASIEREISAWELBA9876543210"
    CONTENT_TYPE = "multipart/form-data; boundary=#{ BOUNDARY }"
    HEADER = { "Content-Type" => CONTENT_TYPE, "User-Agent" => USERAGENT }

    def self.prepare_query(params)
      fp = []

      params.each do |k, v|
        # Are we trying to make a file parameter?
        if v.respond_to?(:path) and v.respond_to?(:read) then
          fp.push(FileParam.new(k, v.path, v.read))
        # We must be trying to make a regular parameter
        else
          fp.push(StringParam.new(k, v))
        end
      end

      # Assemble the request body using the special multipart format
      query = fp.collect {|p| "--" + BOUNDARY + "\r\n" + p.to_multipart }.join("") + "--" + BOUNDARY + "--"
      return query, HEADER
    end
  end

  private

  # Formats a basic string key/value pair for inclusion with a multipart post
  class StringParam
    attr_accessor :k, :v

    def initialize(k, v)
      @k = k
      @v = v
    end

    def to_multipart
      return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"\r\n\r\n#{v}\r\n"
    end
  end

  # Formats the contents of a file or string for inclusion with a multipart
  # form post
  class FileParam
    attr_accessor :k, :filename, :content

    def initialize(k, filename, content)
      @k = k
      @filename = filename
      @content = content
    end

    def to_multipart
      # If we can tell the possible mime-type from the filename, use the
      # first in the list; otherwise, use "application/octet-stream"
      mime_type = MIME::Types.type_for(filename)[0] || MIME::Types["application/octet-stream"][0]
      return "Content-Disposition: form-data; name=\"#{CGI::escape(k)}\"; filename=\"#{ filename }\"\r\n" +
             "Content-Type: #{ mime_type.simplified }\r\n\r\n#{ content }\r\n"
    end
  end
end

你好!这段代码的许可证是什么?另外:在顶部的评论中添加此帖子的URL可能会很好。谢谢! - docwhat
5
此帖子中的代码使用WTFPL许可证(http://sam.zoy.org/wtfpl/)。享受吧! - Cody Brimhall
不应该将文件流传递到FileParam类的初始化调用中。在to_multipart方法中的赋值会再次复制文件内容,这是不必要的!相反,只需传递文件描述符并在to_multipart中从中读取。 - mober
1
这段代码太棒了!因为它能正常工作。Rest-client和Siegers Multipart-post都不支持请求头。如果你需要请求头,使用rest-client和Siegers Multipart post会浪费很多宝贵的时间。 - onknows
实际上,@Onno,它现在支持请求头。请参阅我对Eric答案的评论。 - alexanderbird

19

在尝试过本帖中其他解决方案后,这是我的解决方案。 我使用它来上传照片到TwitPic:

  def upload(photo)
    `curl -F media=@#{photo.path} -F username=#{@username} -F password=#{@password} -F message='#{photo.title}' http://twitpic.com/api/uploadAndPost`
  end

1
尽管看起来有点粗糙,但这对我来说可能是最好的解决方案,非常感谢这个建议! - Bo Jeanes
一个提醒:对于不知情的人,media=@... 是让curl把...看作文件而不是字符串的关键。在Ruby语法中可能会有些混淆,但是@#{photo.path}与#{@photo.path}并不相同。在我看来,这个解决方案是最好的之一。 - Evgeny
9
这看起来很不错,但如果你的 @用户名 包含"foo && rm -rf /",那就会变得非常糟糕 :-P - Anna B

11

7

好的,这里有一个使用 curb 的简单示例。

require 'yaml'
require 'curb'

# prepare post data
post_data = fields_hash.map { |k, v| Curl::PostField.content(k, v.to_s) }
post_data << Curl::PostField.file('file', '/path/to/file'), 

# post
c = Curl::Easy.new('http://localhost:3000/foo')
c.multipart_form_post = true
c.http_post(post_data)

# print response
y [c.response_code, c.body_str]

3

在我重载RestClient :: Payload :: Multipart中的create_file_field之前,restclient对我不起作用。

它在每个部分中创建了一个“Content-Disposition:multipart / form-data”,而应该是“Content-Disposition:form-data”。

http://www.ietf.org/rfc/rfc2388.txt

如果需要,我的分支在这里:git@github.com:kcrawford/rest-client.git


这个问题已经在最新的restclient中得到了修复。 - user243633

1

还有Nick Sieger的multipart-post,可以添加到可能解决方案的长列表中。


1
multipart-post 不支持请求头。 - onknows
实际上,@Onno,它现在支持请求头。请参阅我对Eric答案的评论。 - alexanderbird

1

使用NetHttp的解决方案有一个缺点,即在上传大文件时,它会先将整个文件加载到内存中。

经过一番尝试,我想出了以下解决方案:

class Multipart

  def initialize( file_names )
    @file_names = file_names
  end

  def post( to_url )
    boundary = '----RubyMultipartClient' + rand(1000000).to_s + 'ZZZZZ'

    parts = []
    streams = []
    @file_names.each do |param_name, filepath|
      pos = filepath.rindex('/')
      filename = filepath[pos + 1, filepath.length - pos]
      parts << StringPart.new ( "--" + boundary + "\r\n" +
      "Content-Disposition: form-data; name=\"" + param_name.to_s + "\"; filename=\"" + filename + "\"\r\n" +
      "Content-Type: video/x-msvideo\r\n\r\n")
      stream = File.open(filepath, "rb")
      streams << stream
      parts << StreamPart.new (stream, File.size(filepath))
    end
    parts << StringPart.new ( "\r\n--" + boundary + "--\r\n" )

    post_stream = MultipartStream.new( parts )

    url = URI.parse( to_url )
    req = Net::HTTP::Post.new(url.path)
    req.content_length = post_stream.size
    req.content_type = 'multipart/form-data; boundary=' + boundary
    req.body_stream = post_stream
    res = Net::HTTP.new(url.host, url.port).start {|http| http.request(req) }

    streams.each do |stream|
      stream.close();
    end

    res
  end

end

class StreamPart
  def initialize( stream, size )
    @stream, @size = stream, size
  end

  def size
    @size
  end

  def read ( offset, how_much )
    @stream.read ( how_much )
  end
end

class StringPart
  def initialize ( str )
    @str = str
  end

  def size
    @str.length
  end

  def read ( offset, how_much )
    @str[offset, how_much]
  end
end

class MultipartStream
  def initialize( parts )
    @parts = parts
    @part_no = 0;
    @part_offset = 0;
  end

  def size
    total = 0
    @parts.each do |part|
      total += part.size
    end
    total
  end

  def read ( how_much )

    if @part_no >= @parts.size
      return nil;
    end

    how_much_current_part = @parts[@part_no].size - @part_offset

    how_much_current_part = if how_much_current_part > how_much
      how_much
    else
      how_much_current_part
    end

    how_much_next_part = how_much - how_much_current_part

    current_part = @parts[@part_no].read(@part_offset, how_much_current_part )

    if how_much_next_part > 0
      @part_no += 1
      @part_offset = 0
      next_part = read ( how_much_next_part  )
      current_part + if next_part
        next_part
      else
        ''
      end
    else
      @part_offset += how_much_current_part
      current_part
    end
  end
end

什么是StreamPart类? - Marlin Pierce

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接