gpt4 book ai didi

ios - rails 4 后端和 ios 图片上传

转载 作者:可可西里 更新时间:2023-11-01 06:15:30 24 4
gpt4 key购买 nike

我想通过 web 或 ios 应用程序构建一个图片上传购买 jar ,我有一个例子 https://github.com/defish16/ios-to-rails-images , 我使用 Rails 4

型号:

class Test < ActiveRecord::Base
attr_accessor :avatar_data

has_attached_file :avatar, :styles => { medium: ["300x300>", :png], thumb: ["100x100>", :png]}
belongs_to :project
before_save :decode_avatar_data

def decode_avatar_data
# If avatar_data is present, it means that we were sent an image over
# JSON and it needs to be decoded. After decoding, the image is processed
# normally via Paperclip.
if self.avatar_data.present?
data = StringIO.new(Base64.decode64(self.avatar_data))
data.class.class_eval {attr_accessor :original_filename, :content_type}
data.original_filename = self.id.to_s + ".png"
data.content_type = "image/png"

self.avatar = data
end
end

end

class Project < ActiveRecord::Base
has_many :avatars, class_name: 'Test', dependent: :destroy
accepts_nested_attributes_for :avatars
end

Controller

class ProjectsController < ApplicationController

def create
@project = Project.new(project_params)

respond_to do |format|
if @project.save
format.html { redirect_to @project, notice: 'Project was successfully created.' }
format.json { render action: 'show', status: :created, location: @project }
else
format.html { render action: 'new' }
format.json { render json: @project.errors, status: :unprocessable_entity }
end
end
end

def project_params
params.require(:project).permit(:name, :description, avatars_attributes: [:avatar])
end

class TestsController < ApplicationController

def create
@test = Test.new(test_params)

respond_to do |format|
if @test.save
format.html { redirect_to @test, notice: 'Test was successfully created.' }
format.json { render action: 'show', status: :created, location: @test }
else
format.html { render action: 'new' }
format.json { render json: @test.errors, status: :unprocessable_entity }
end
end
end

def test_params
params.require(:test).permit(:project, :avatar_data, :avatar)
end

我可以通过网络正常上传,但是当我尝试通过 ios 应用程序上传时,我无法获取图像

这是我的部分日志

当我使用 ios 应用程序时参数:{"avatars_attributes"=>[{"avatar_data"=>"/9j/4AAQSkZJRgABAQAAAQABAAD/4QBYR......

但是在网络中参数:{"project"=>{"name"=>"test3", "description"=>"teset3", "avatars_attributes"=>{"0"=>{"avatar"=>#

这是个问题吗?因为应用程序中的 avatars_attributes 不在“项目”中,我该如何解决?在应用程序或服务器中如果不是,哪里会出错??

最佳答案

您打算解码模型中的 base64 图像数据 url,但您的 View 中似乎没有 base64 数据 url,我有一个示例来说明如何将图像文件转换为数据 url(使用 html5 文件 api,不起作用< IE10):

http://codepen.io/luckyyang/pen/BghJD

关于ios - rails 4 后端和 ios 图片上传,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19851627/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com