Curl请求将图像从本地机器上传到ruby on rails应用程序 [英] Curl request to upload image from local machine to ruby on rails application

查看:100
本文介绍了Curl请求将图像从本地机器上传到ruby on rails应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个应用程序内置在ruby在rails。我需要发布curl请求该应用程序从本地机器上传图像。在我的ruby在rails应用程序我使用回形针图像上传。



现在这个curl请求工作完全正常,因为没有上传图像此卷曲请求: p>

  curl -v -HAccept:application / json-HContent-type:application / json-X PUT -d '{user:{first_name:John,last_name:Smith},token:api} http:// localhost:3000 / users / 8 

现在我要从我的localmachine上传图片。



display_photo:@ test.jpg is not work:这是我正在尝试:( 但它不工作。)

  curl -v -H -FAccept:application / json-HContent-type:application / json-X PUT -d'{user:{first_name:firstname ,last_name:lastname,display_photo:@ test.jpg},token:api}'http:// localhost:3000 / users / 8 



所以问题是如何从curl请求上传图片/数据文件/视频



EDIT



这是我的控制器:

  def update 
@user = User.find(params [:id])

respond_to do | format |
if @ user.update_attributes(params [:user])
format.html {redirect_to(@user,:notice =>'User was successfully updated。')}
format.json {render:file => users / json / show.json.erb,:content_type => 'application / json'}
else
format.html {render:action => edit}
format.json {
@errors_object = @user
render:file => shared / json / errors.json.erb,:content_type => 'application / json'}
end
end
end

在我的模型中:

  class User< ActiveRecord :: Base 
has_attached_file:display_photo,:styles => {:medium => 200x200#,:thumb => 100x100#,:very_small =>24x24#}
end


解决方案

您通常会上传这样的文件:

  curl -i -F filedata = @ upload.txt http:// localhost:5000 / 

这样做模仿了一个webbrowsers文件上传。



如果你看到rails响应这个,它通过 application / octet-stream发送可以作为上传的文件正确处理。只要我知道不可能这样做与一个json请求:

 启动POST/为127.0.0.1在2012-07-10 09:40:59 +0200 
HomeController#index as * / *
处理参数:{filedata=>#< ActionDispatch :: Http :: UploadedFile:0x007f82d59a3580 @ original_filename =upload.txt,@ content_type =application / octet-stream,@ headers =Content-Disposition:form-data; name = \filedata\; filename = \upload.txt \\r\\\
Content-Type:application / octet-stream\r\\\
,@tempfile =#< File:/ var / folders / f_ / wjnrg7cd3d9f1tpy3k5fhrwm0000gn / T / RackMultipart20120710-30471-1t9abns> >}


I have an application built in ruby on rails. I need to post curl request to that application to upload images from local machine. In my ruby on rails application I am using paperclip for image uploader.

Now this curl request is working perfectly fine as there is no upload image for this curl request:

  curl -v -H   "Accept: application/json" -H "Content-type: application/json" -X PUT -d '{"user":{"first_name":"John","last_name":"Smith"},"token":"api"}'  http://localhost:3000/users/8

Now I want to upload an image from my localmachine.

Adding "display_photo":@test.jpg is not working: This is what I was trying: (But its not working.)

  curl -v -H  -F "Accept: application/json" -H "Content-type: application/json" -X PUT -d '{"user":{"first_name":"firstname","last_name":"lastname","display_photo":@test.jpg},"token":"api"}'  http://localhost:3000/users/8

So the question is how to upload images/data files/ videos from curl request

EDIT

This is my controller:

   def update
    @user = User.find(params[:id])

    respond_to do |format|
      if @user.update_attributes(params[:user])
        format.html {  redirect_to(@user, :notice => 'User was successfully updated.') }
        format.json  { render :file => "users/json/show.json.erb", :content_type => 'application/json' }
      else
        format.html { render :action => "edit" }
        format.json  { 
          @errors_object = @user
          render :file => "shared/json/errors.json.erb", :content_type => 'application/json' }
      end
    end
  end

In my model:

class User < ActiveRecord::Base
    has_attached_file :display_photo, :styles => { :medium => "200x200#", :thumb => "100x100#", :very_small=>"24x24#" }
end

解决方案

you would normally upload a file like this:

curl -i -F filedata=@upload.txt http://localhost:5000/

doing so mimics the behavior that you would have through a webbrowsers file upload.

if you look at the rails response for this, it's sent via application/octet-stream that can be handled properly as an uploaded file. as far as i know it's not possible to do this with a json request:

Started POST "/" for 127.0.0.1 at 2012-07-10 09:40:59 +0200
Processing by HomeController#index as */*
  Parameters: {"filedata"=>#<ActionDispatch::Http::UploadedFile:0x007f82d59a3580 @original_filename="upload.txt", @content_type="application/octet-stream", @headers="Content-Disposition: form-data; name=\"filedata\"; filename=\"upload.txt\"\r\nContent-Type: application/octet-stream\r\n", @tempfile=#<File:/var/folders/f_/wjnrg7cd3d9f1tpy3k5fhrwm0000gn/T/RackMultipart20120710-30471-1t9abns>>}

这篇关于Curl请求将图像从本地机器上传到ruby on rails应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆