Sending <input type=“image”> to a folder on

2020-02-15 14:49发布

I have this code

<%= form_for(:img, url: {action: "postodb"}) do |f| %>
<div id="image-preview">image here!</div>
<%= f.submit "Send to database" %>
<%end%>

Here <input type="image"> is added from a js file

var imageDiv = $("#image-preview");
  imageDiv.html('<input type="image" name="img1" style="margin-left:146px"id="spiro-img" src="'+img+'"/>');

This all works fine..

Next I want to send this to the folder but it doesnt work

This is the code I have in the controller(referred from this site http://rohitrox.github.io/2013/07/19/canvas-images-and-rails/)

def postodb
data = params[:data_uri]
image_data = Base64.decode64(data['data:image/png;base64,'.length .. -1])

File.open("/public/uploads/somefilename.png", 'wb') do |f|
  f.write image_data
end
end

This is the error I get

enter image description here

Plz help.

2条回答
手持菜刀,她持情操
2楼-- · 2020-02-15 15:28

Using carrierwave gem for loading images.

查看更多
3楼-- · 2020-02-15 15:42

First what your should to do is read a docs: http://www.w3schools.com/Tags/att_input_type.asp

image - Defines an image as the submit button.

Second what your should learn to read is logs. At screenshot you have section with params, and there is no data_uri key.

And finaly use the <input type='file' ... /> for upload. If you want beauty async upload with preview, you should look on jquery-file-upload gem instead.

P.S.: type='image' is a submit button with image.

查看更多
登录 后发表回答