作者热门文章
- xml - AJAX/Jquery XML 解析
- 具有多重继承的 XML 模式
- .net - 枚举序列化 Json 与 XML
- XML 简单类型、简单内容、复杂类型、复杂内容
我想创建一个图片 uploader 。用户可以将 url 粘贴到随后上传的图像,也可以从本地计算机上传图像。我应该如何验证图像?
我如何使用回形针或其他一些图片上传 gem 创建它?
我的观点:
<%= simple_form_for [:admin, @konkurrancer], :html => { :multipart => true } do |f| %>
<%= f.input :name, :label => 'Titel', :style => 'width:500;' %>
<%= f.file_field :photo, :label => '125x125', :style => 'width:250;' %> or
<%= f.input :photo, :label => '125x125', :style => 'width:500;' %>
<%= f.button :submit, :value => 'Create item' %>
<% end %>
最佳答案
这是一种实现远程上传的方法(并且仍然使用回形针):
像这样创建一个类:
require 'open-uri'
# Make it always write to tempfiles, never StringIO
OpenURI::Buffer.module_eval {
remove_const :StringMax
const_set :StringMax, 0
}
class RemoteUpload
attr_reader :original_filename, :attachment_data
def initialize(url)
# read remote data
@attachment_data = open(url)
# determine filename
path = self.attachment_data.base_uri.path
# we need this attribute for compatibility to paperclip etc.
@original_filename = File.basename(path).downcase
end
# redirect method calls to uploaded file (like size etc.)
def method_missing(symbol, *args)
if self.attachment_data.respond_to? symbol
self.attachment_data.send symbol, *args
else
super
end
end
end
编辑
您可以向您的模型添加一个虚拟属性,例如 photo_remote_url:
class YourModel < ActiveRecord::Base
attr_accessor :photo_remote_url
def photo_remote_url=(url)
return if url.blank?
self.photo = RemoteUpload.new(url)
end
end
关于ruby-on-rails - Rails如何创建多选图片上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6033690/
我正在尝试用 Swift 编写这段 JavaScript 代码:k_combinations 到目前为止,我在 Swift 中有这个: import Foundation import Cocoa e
我是一名优秀的程序员,十分优秀!