2017-04-27 79 views
1

我有一個反應本機應用程序從設備讀取圖像文件作爲base64字符串,並通過axios將其提交到rails應用程序。以下是正在提交的數據的一部分:如何將base64編碼文件提交到回形針?

/9j/4QBYRXhpZgAATU0AKgAAAAgABIdpAAQAAAABAAAAPgESAAQAAAABAAAAAAEBAAQAAAABAAAB4AEAAAQAAAABAAACgAAAAAAAAZIIAAQAAAABAAAAAAAAAAD/4AAQSkZJRgABAQAAAQABAAD/2wBDAAYEBQYFBAYGBQYHBwYIChAKCgkJChQODwwQFxQYGBcUFhYaHSUfGhsjHBYWICwgIyYnKSopGR8tMC0oMCUoKSj/2wBDAQcHBwoIChMKChMoGhYaKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCgoKCj/wAARCAHgAoADASIAAhEBAxEB/8QAGgABAQEBAQEBAAAAAAAAAAAAAAcGBQECCP/EAEIQAQAAAAcMBwQKAwEBAQAAAAAHERJFg8LSAgMEBQYVFhdUVZKjARMUMmWU0SMxpOIhIiQlJidDUWFxN0GBkbEz/8QAGAEBAQEBAQAAAAAAAAAAAAAAAAUGBAH/xAAmEQEAAAIKAgMBAAAAAAAAAAAAgaEFERUWQkNiY8HhBAYSFCEi/9oADAMBAAIRAxEAPwD8vjbatccbTgHHd2TVrjjacA47uyDEjbatccbTgHHd2TVrjjacA47uyDpQNzxQ11JTbE35e9dnr2/b5PVdk+vJkRxypUmLv9EXv/59EfT1lYn2bD+C4tA2wxOsrE+zYfwXFo1lYn2bD+C4tA2wxOsrE+zYfwXFo1lYn 

問題是,我無法直接將此添加到回形針。我應該怎麼做才能使它工作?在模型

+1

要downvoter:請考慮您解釋選擇 – Cherniv

回答

0

方法附加文件(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

+0

您好,我得到了一個錯誤說'回形針:: AdapterRegistry :: NoHandlerError(無處理髮現「/ 9J/4QBYRXhpZgAATU0AKgAAAAgABIdpAAQAAAABAAAAPgESAAQ ...' – THpubs

+0

嗯....真回形針。所以BASE64,/ 9J/4QBYRXhpZgAATU0AKgAAAAgABIdpAAQAAAABAAAAPgESAAQ ...' ;圖像/ JPEG:不會與這樣的數據的工作你picture_64應該有一些額外的信息喜歡的東西前綴回形針 有效的信息類型看起來像 '數據。你需要知道文件類型 模式如下: 'data:#{file_type}; base64,#{encoded64_file}' 這裏是[spec](https://github.com/sithob) og/events/blob/master/spec/controllers/api/v1/events_controller.rb)從回答測試控制器。 – nautgrad

+0

此解決方案僅適用於** Paperclip v 5.0.0 ** – m1l05z

相關問題