How to upload a file in ruby on rails?

后端 未结 4 2070
有刺的猬
有刺的猬 2021-02-01 06:16

I’m very new in ruby on rails. I’m stuck with a problem. I want to make a file upload functionality through which I can upload any kind of file (text,image etc.). My controller

4条回答
  •  悲&欢浪女
    2021-02-01 06:31

    The reason for the issue is encoding problems. It seems that you are reading the file in ASCII-8BIT mode and writing it in UTF-8 which means a conversion needs to take place. And conversion from ASCII-8BIT to UTF-8 isn't straight forward. Alternatively, you can specify binary mode for both reading and writing the files.

    upload_file = File.new(, "rb").read
    

    and

    File.open(, "wb") {|f| f.write(upload_file) }
    

提交回复
热议问题