-2

I want to create API for upload photo in rails app for android mobile app.

I have no idea about how to create API for photo upload.

def update # scoped to current_user
    @user = @current_user

    @user.update(:portrait => File.open(params[:portrait], 'rb'))

    render action: 'settings'
end

I tried above code to update profile picture but it raises following exception:

Errno::ENOENT (No such file or directory @ rb_sysopen - https://res.cloudinary.com/hmdmd2cr2/image/upload/c_fill,g_face,h_256,w_256/v1525241327/dy8hbwqjedfhtaaycpza.jpg):
Surya
  • 15,703
  • 3
  • 51
  • 74
Pooja Mokariya
  • 1,970
  • 5
  • 21
  • 46
  • Where are you stuck? What's the first problem? – Jagdeep Singh May 04 '18 at 05:32
  • @JagdeepSingh i updated my question please refer and help me. – Pooja Mokariya May 04 '18 at 05:42
  • @P_M - It is apparent that you're trying to open a file using `File` class while file is on a different server, either store the file locally first or just store the URI of file to users column if you own this image storage service. – Surya May 04 '18 at 05:48
  • i dont know what to use for upload file on server from system – Pooja Mokariya May 04 '18 at 05:56
  • The error suggests to me you are passing a path on cloudanary as a string in the `portrait` param. If you want to get that file and and store it as a blob in the DB I would suggest using `open-uri` to do so. [This SO question helps with that](https://stackoverflow.com/questions/2515931/how-can-i-download-a-file-from-a-url-and-save-it-in-rails#2517286). An approach I would suggest is rather than storing the blob in the db is to just store the string path of the file as @Surya, or you could use a gem like [paperclip](https://github.com/thoughtbot/paperclip). – Dave K May 04 '18 at 07:38
  • @DaveK I want API to upload image on server not to download – Pooja Mokariya Jun 15 '18 at 06:44
  • @Surya I want API to upload image on server not to download – Pooja Mokariya Jun 15 '18 at 06:45

1 Answers1

0

You need to pass a valid path to File.open a valid so that it can write the file.

Try

  File.open(Rails.root.join('public', 'uploads', params[:portrait].original_filename), 'wb') do |file|
    file.write(params[:portrait].read)
  end
Paul Byrne
  • 1,563
  • 18
  • 24