要导入到每张照片的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
{"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