最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How I put the canvas content as a file in a input type file - Stack Overflow

programmeradmin3浏览0评论

After months of searching examples and answers, I can’t find anything that works for me. All help is wele.

System --- Woomerce -- Single product page.

I bought an add-on to attach one image when your're on <form class="cart">. This is in single product page.

Basically the plugin adds a <input type ="file"> in the form and allows you to attach images in order. I'm happy with it because it works fine and a lot of backend work is taken care of.

On the other hand, I have a <canvas> and I need yo load the <canvas> content in the <input type ="file">. Once done, the add-on is ready to do its job.

So my question is: How do I put the content of the canvas as a file in the <input>?

After months of searching examples and answers, I can’t find anything that works for me. All help is wele.

System --- Woomerce -- Single product page.

I bought an add-on to attach one image when your're on <form class="cart">. This is in single product page.

Basically the plugin adds a <input type ="file"> in the form and allows you to attach images in order. I'm happy with it because it works fine and a lot of backend work is taken care of.

On the other hand, I have a <canvas> and I need yo load the <canvas> content in the <input type ="file">. Once done, the add-on is ready to do its job.

So my question is: How do I put the content of the canvas as a file in the <input>?

Share Improve this question edited Nov 17, 2020 at 13:50 allenski 1,8624 gold badges29 silver badges40 bronze badges asked Nov 16, 2020 at 16:42 DavidDavid 231 silver badge5 bronze badges 3
  • You can convert the canvas to a base64 image path and use that. developer.mozilla/en-US/docs/Web/API/HTMLCanvasElement/… – Christopher Marshall Commented Nov 16, 2020 at 16:57
  • step 1: ask youself why you need the canvas content: did you draw an image onto it? just use that image. Step 2: if you must use the canvas, you will only be able to use it if the content you draw on it is all "clean" data (as per CSP). If you drew images from a third party site that hasn't explicitly whitelisted you: too bad, you can't access the canvas' data. Step 3: given confirmation in step 2, use the toDataURL function to turn the canvas into an image, just data-url encoded, and put that in your input. – Mike 'Pomax' Kamermans Commented Nov 16, 2020 at 16:57
  • After toDataURL I get base64 data from canvas, then, I need put it on as a file in file type input but I don't know how. I've tried setting input.value equal to this data getted but don't result. – David Commented Nov 16, 2020 at 19:32
Add a ment  | 

1 Answer 1

Reset to default 7

The best is still to send files through a FormData and AJAX.

But since it seems it's for personal use, and that you already have something working with a <form>, you can probably use what is still an hack exposed in this answer of mine. Beware this still works only in latest Chromium and Geckos browsers (no Webkit support, which means no iOS support).

So the step by step is,

  • draw on your canvas.
  • export it to a Blob using the HTMLCanvasElement.toBlob() method.
  • build a File from this Blob
  • build a DataTransfer object
  • append the File to the DataTransfer's items list
  • set the .files property of your <input type="file"> to the DataTransfer's .files

// draw on the canvas
const canvas = document.createElement( "canvas" );
const ctx = canvas.getContext( "2d" );
ctx.fillStyle = "red";
ctx.fillRect( 20, 20, 260, 110 );

// convert to Blob (async)
canvas.toBlob( (blob) => {
  const file = new File( [ blob ], "mycanvas.png" );
  const dT = new DataTransfer();
  dT.items.add( file );
  document.querySelector( "input" ).files = dT.files;
} );

// to prove the image is there
document.querySelector( "#test" ).onclick = (evt) => {
  const file = document.querySelector( "input" ).files[ 0 ];
  document.body.appendChild( new Image() )
    .src = URL.createObjectURL( file );
};
<form method="POST">
  <input type="file" name="file"><br>
  <button>submit</button> (check your dev tools network panel to see the File is sent)
</form>
<br>
<button id="test">
  load input's content as image
</button>

发布评论

评论列表(0)

  1. 暂无评论