Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to upload signature as image file / append to FormData? #35

Closed
whitesnow9291 opened this issue Nov 7, 2019 · 1 comment
Closed
Labels
kind: question kind: support Asking for support with something or a specific use case problem: stale Issue author has not responded

Comments

@whitesnow9291
Copy link

After drawing, how can it be append to formdata?

@agilgur5
Copy link
Owner

agilgur5 commented Nov 7, 2019

Ah, good usage question! There's a small bit about this in signature_pad's README, but it doesn't go too into detail.

To begin, you could upload the image as a blob, or you could upload it as a dataURL (the above link goes over how you might decode a dataURL server-side). As a dataURL, it's just a very long string, so sending that to the server is, for the most part, like POSTing any other string.

As a blob, yes you can use FormData for that. To get the blob, you'll have to get the raw Canvas element from a ref to react-signature-canvas. For instance, if you're using class components and your ref is stored in this.sigCanvas like some of the examples, you would use:

const canvas = this.sigCanvas.getCanvas()

Once you've got the canvas, one option to transform it into a blob is through the canvas.toBlob function. It's important to note that toBlob is not supported in all browsers, so you might want to polyfill it with something like blueimp-canvas-to-blob depending on what type of users you need to support. With toBlob, you can then do:

canvas.toBlob((blob) => {
  const fd = new window.FormData()
  fd.append('signature', blob, 'signature.png')
  // POST to server with your preferred requests library
})

Alternatively, blueimp-canvas-to-blob also exports dataURLtoBlob, so if you were to use that polyfill/library and that function, you could instead do:

import dataURLtoBlob from 'blueimp-canvas-to-blob'

// ...

const blob = dataURLtoBlob(canvas.toDataURL('image/png'))
const fd = new window.FormData()
fd.append('signature', blob, 'signature.png')
// POST to server with your preferred requests library

There are of course other ways of doing it, but hope those examples help or point you in the right direction at least 🙂

@agilgur5 agilgur5 closed this as completed Nov 7, 2019
@agilgur5 agilgur5 changed the title How can Upload as image file? How to upload signature as image file / append to FormData? Dec 23, 2019
@agilgur5 agilgur5 added kind: support Asking for support with something or a specific use case problem: stale Issue author has not responded labels Mar 13, 2021
@agilgur5 agilgur5 changed the title How to upload signature as image file / append to FormData? How to upload signature as image file / append to FormData? Sep 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind: question kind: support Asking for support with something or a specific use case problem: stale Issue author has not responded
Projects
None yet
Development

No branches or pull requests

2 participants