Uploading a remote file url from Rails Console with Carrierwave

后端 未结 5 710
不思量自难忘°
不思量自难忘° 2020-12-25 10:46

I just wanted to know how one would go about uploading a remote file url using Carrierwave in the Rails console.

I tried the following without any luck. I presume it

5条回答
  •  清歌不尽
    2020-12-25 11:22

    Is work as:

    url='http://host.domain/file.jpg'    
    time=Time.now.to_i.to_s
    myfile=IO.sysopen("tmp/"+time+"_img."+url.split(".").last,"wb+")
    tmp_img=IO.new(myfile,"wb")
    tmp_img.write open(URI.encode(url)).read
    
    if File.exist?("tmp/"+time+"_img."+url.split(".").last)
      "tmp/"+time+"_img."+url.split(".").last
      image = ActionDispatch::Http::UploadedFile.new(:tempfile => tmp_img, :filename => File.basename(tmp_img))
    else 
      image=nil
    end
    @your_model.image=image
    @your_model.save
    

提交回复
热议问题