用百度开源webuploader 上传图片 rubyonrails 后端代码

要导入到每张照片的params 是这样的:


{"name"=>"1 (1).jpg", 
"size"=>"164623", 
"lastModifiedDate"=>"Sat Oct 24 2014 12:20:34", 
"id"=>"WU_FILE_0", 
"type"=>"image/jpeg", 
"my_id"=>"1293", 
"file"=>#<File:C:/Users/ror_licker/AppData/Local/Temp/CGI2141217-1856-1smk4wo-0>, 
"file_sw"=>"1"}


upload_controller.rb:
def upload_files
      filename = params[:name].to_s
      file_id = params[:id]
      file = params[:file]


      if  filename.index '(' 
        itemno = lsplit(filename,'(')
      else
        itemno = lsplit(filename,'.')
      end


      if append_picture(file_id,filename,file)
         render :json=>{"jsonrpc" => "2.0", "result" => "confirm", "id" => "file_id"}
      else
         render :json=>{"jsonrpc" => "2.0", "error" =>{"code"=> 103, "message"=>"导入图片失败"}, "id"=>"file_id"}
      end


end


 def append_picture(object_id, fn,file)
    if file && file.size >0
      ptemp = exist_one(object_id,fn)
      if ptemp
        picture = ptemp
      else
        picture = Objectpicture.new
      end
      
      picture.auctionobject_id  = object_id   
      picture.photo = file
      picture.update_time = Time.now  


      if !picture.save
        logger.info "#{object_id.to_s},  save error"
        puts "#{object_id.to_s},  save error"
        return false
      else
        puts "=== import OK: #{object_id}"
        return true
      end
    else
      return false
    end
  end


  def lsplit(meat,separator)
    knife =  meat.to_s.index(separator) if separator
    meat = meat[0,knife.to_i] if meat
    return meat
  end

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值