我有一个react本机应用程序,它将设备中的图像文件作为base64
字符串读取,并通过axios
提交给rails
应用程序。以下是提交数据的一部分:
/9j/4QBYRXhpZgAATU0AKgAAAAgABIdpAAQAAAABAAAAPgESAAQAAAABAAAAAAEBAAQAAAABAAAB4AEAAAQAAAABAAACgAAAAAAAAZIIAAQAAAABAAAAAAAAAAD/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAHgAoADASIAAhEBAxEB/8QAGgABAQEBAQEBAAAAAAAAAAAAAAcGBQECCP/EAEIQAQAAAAcMBwQKAwEBAQAAAAAHERJFg8LSAgMEBQYVFhdUVZKjARMUMmWU0SMxpOIhIiQlJidDUWFxN0GBkbEz/8QAGAEBAQEBAQAAAAAAAAAAAAAAAAUGBAH/xAAmEQEAAAIKAgMBAAAAAAAAAAAAgaEFERUWQkNiY8HhBAYSFCEi/9oADAMBAAIRAxEAPwD8vjbatccbTgHHd2TVrjjacA47uyDEjbatccbTgHHd2TVrjjacA47uyDpQNzxQ11JTbE35e9dnr2/b5PVdk+vJkRxypUmLv9EXv/59EfT1lYn2bD+C4tA2wxOsrE+zYfwXFo1lYn2bD+C4tA2wxOsrE+zYfwXFo1lYn
问题是,我不能直接将它添加到回形针。我该怎么做才能让它发挥作用?
答案 0 :(得分:3)
模型中附加文件的方法(在此示例中为Event
):
def add_picture(picture_64, picture_name)
picture_data = Paperclip.io_adapters.for(picture_64)
picture_data.original_filename = picture_name
self.picture = picture_data
end
控制器中的方法:
def create_picture
if params[:picture]
picture_params = params[:picture]
@event.add_picture(picture_params[:file_data], picture_params[:file_name])
end
end
一般来说,您只需要Paperclip.io_adapters.for(picture_64)
,这将产生Paperclip
可接受的必要对象。