【问题标题】:How to upload image from mobile in rails app and carrierwave uploader如何在rails应用程序和carrierwave上传器中从手机上传图片
【发布时间】:2014-09-15 17:39:47
【问题描述】:

我们在 ruby​​ on rails 中有一个 Web 应用程序。现在我们正在开发 API 和一个适用于 android 和 iphone 的应用程序。我们将钛用于移动应用程序。在上传文件的 Web 应用程序中,我们使用载波。我正在按照 railscast 中所示的相同过程使用carrierwave上传文件。我的问题是如何在移动应用程序中做到这一点。如果可能的话,我需要想法和示例代码。以下是我的代码,适用于应用程序的 Web 版本。

控制器动作

 def create_file
    @community_message_file = Community::MessageFile.new(params[:community_message_file])

    respond_to do |format|
      if @community_message_file.save
        format.js {}
      else
        flash.now[:error] = @community_message_file.errors.size > 0 ? ErrorUtility.validation_errors(@community_message_file.errors) :
            "Saving failed"
        format.js {}
      end
    end
  end

一些方法模型

mount_uploader :file_path, MessageFileUploader

  validates :file_path, file_size: {maximum: 1.megabytes.to_i}

  def extension
    return extract_extension
  end

  def file_name
    return File.basename(self.file_path.to_s)
  end

  def image?
    file_extension = extract_extension
    return true if (file_extension == 'jpeg' || file_extension == 'jpg' || file_extension == 'png' || file_extension == 'gif')
    return false
  end

  private

  def extract_extension
    return File.extname(self.file_path.to_s).split('.').last
  end

还有我的上传者模型

include CarrierWave::MiniMagick
  after :remove, :clear_uploader
storage :file
def store_dir
    "uploads/#{model.class.to_s.underscore}/#{mounted_as}/#{model.id}"
  end

  def clear_uploader
    @file = @filename = @original_filename = @cache_id = @version = @storage = nil
    model.send(:write_attribute, mounted_as, nil)
  end

def extension_white_list
     %w(jpg jpeg gif png doc docx xls xlsx pdf zip)
  end

def image?(new_file)
    %w(jpg jpeg gif png).include?(new_file.extension.to_s)
  end

【问题讨论】:

    标签: ruby-on-rails api file-upload carrierwave titanium-mobile


    【解决方案1】:

    刚刚使用 ios 模拟器进行了测试。通过文件字段,移动 Safari 会提示照片应用程序选择照片。从那里你如何在后端实现它是无关紧要的。浏览器请求的工作方式与桌面相同。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-01-31
      • 1970-01-01
      • 1970-01-01
      • 2014-02-12
      • 1970-01-01
      • 2017-03-02
      • 2023-03-26
      • 1970-01-01
      相关资源
      最近更新 更多