我正在尝试使用PUT将一个文件上传到webmachine资源。其想法是用一个file_id更新模板资源。
module App::Resources
class UpdateTemplateResource < TemplateResource
def allowed_methods
%W(PUT)
end
def content_types_accepted
# What to do here?
end
private
def template_id
request.path_info[:id]
end
def template
@template ||= ::App::Models::Template.find_latest_version_by_guid(id)
end
end
end我已经找到了接受json类型请求的示例,但没有接受multipart。该文件不是保存在服务器中,而是转换后发送到另一个服务以进行存储。
发布于 2016-05-04 17:33:48
Webmachine::Request对象包含主体,实质上是具有边界的多部分请求。如果我们知道要发送哪种类型的文件,就可以解析它。
主体边界包括与其关联的内容类型、文件名和param。然后启动实际文件。
如果JSON
lines = []
request.body.to_io.each {|l| lines << l if l =~ /\[/ }
json = JSON.parse(lines[0])If pdf文件
lines = request.body.to_io.read
pdf_as_string = lines.match(/^(\%PDF-)(.*\s)*(\%\%EOF\s)$/)[0]https://stackoverflow.com/questions/36996099
复制相似问题