1

I have this simple upload form to browse and upload an image.

<form action="https://storage.googleapis.com/YOUR_BUCKET_NAME" 
      method="post" enctype="multipart/form-data">
  <input name="key" type="text" value="objectName.txt" /><br/>
  <input name="file" type="file" /><br/>
  <input type="submit" value="Upload!" />
</form>

But I'm using an image cropper, that outputs a base64 string and want to upload that instead.

Now the browser doesn't allow me to set the value of the file input to the string, it wants a file.

Now I replaced:

<input name="file" type="file" />

With:

<input type="hidden" name="file" />

And handle the browsing/picking an image elsewhere.

Now this somehow does upload a file, but the image is invalid, it's just a text-file with the base64 string inside it. Changing the type to file will fail the upload.

So I'm guessing it has to do with the base64 having to be converted to a readable file.

After searching for converting base64 to file, I've tried:

blobData = atob(imageData)

But it errors: Uncaught InvalidCharacterError: Failed to execute 'atob' on 'Window': The string to be decoded is not correctly encoded., while if I paste the string (data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAMgAAADICAYAAACtW....etc) into the address-bar it displays correctly. Same when I try this: http://wiki.lenux.org/base64-string-to-blob-object/ I there's a lot of different ways to convert it, but I keep getting the error on atob that it's not correctly encoded.

I feel there must be a simple solution, because the browser already knows how to decode the string.

I've found: http://www.nixtu.info/2013/06/how-to-upload-canvas-data-to-server.html that outputs only the size and type of the image, but does not convert it. And setting it as the value directly will result in a string: '[object Blob]`

Going deeper down the rabbithole: Convert Blob to binary string synchronously this converts the blob to a Uint8Array, I think I'm very close, but I'm lost right now.

To be clear, I'm looking for a client-side solution, and don't want to use canvas or any other extra helper elements.

Community
  • 1
  • 1
TrySpace
  • 2,233
  • 8
  • 35
  • 62
  • Are you trying to upload `data URI` to server, or convert `data URI` to a `File` object? – guest271314 Apr 12 '16 at 15:56
  • See http://stackoverflow.com/questions/28856729/upload-multiple-image-using-ajax-php-and-jquery/ – guest271314 Apr 12 '16 at 16:03
  • @guest271314, convert it, uploading works, but now the file is a text-file with the raw data in it. – TrySpace Apr 12 '16 at 16:40
  • It is not possible to set a `File` object as a value of `FileList` object at `input type="file"` element. You can use AJAX to upload `FormData` object or `data URI` representation of file. – guest271314 Apr 12 '16 at 16:45
  • See http://stackoverflow.com/questions/4998908/convert-data-uri-to-file-then-append-to-formdata – guest271314 Apr 12 '16 at 16:54

1 Answers1

2

You are trying to convert the URI scheme instead of the data. You will have to get at the encoded data of the URI to decode it.

data URI syntax

Let's take a look again at this img tag and its slightly disturbing syntax:

<img src="data:image/png;base64,iVBOR..." />

You have:

  • data - name of the scheme
  • image/png - content type
  • base64 - the type of encoding used to encode the data
  • iVBOR... - the encoded data
  • some , and ; and : sprinked for good measure

http://www.phpied.com/data-urls-what-are-they-and-how-to-use/

The easiest way would be to split on the comma.

var image ="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAASwAAABTCAMAAAASoMrnAAAC/VBMVEUiHR4lISIoJCUqJygtKSoxLS4yLzA5NTY6Njg8OTpAPT5CP0BEQUJIRUZKRkhMSUpRT1BUUVJYVVZZV1hcWVpgXV5iX2BkYmJoZWZpZ2hsamtwbm5xb3B4dnZ5d3iAfX6mim6njHGojXGqkXatlHqvmH6xl36Cg4iIhYaJh4iLioqRjY6Qj5CUkpSYlpacmpucnqKfoKOgnp6zm4Ogn6C2oIi4o427pZC9qJKjoqOjpKilpqmopqepp6itrbCwrq+0s7S4tra5t7i8u7vBrZrDsJ3It6fKuanAv8DRw7TXyr7YzL/Ix8fIx8jQzs/b0MXd08jT1NXU09PU09TY19jZ2dnc2tvc29ze3d3g183h2M3k3NTg3t/g3+Dn4Nrp4tzk4+Tq5+Po5+ju6ePs6+zx7ejw7/D08O7z8vL////9/Pr8+PTTiyj+iAj++O/8+vn79Ov97Nv+wHy/l1LqyZzv1LH+jRP9lSP87uLWkTPYmUH88OTFnWHAlVP9lUT69vH06tv8+/z9rFL95Mv+2bz26dX+zJX+z5vk0rZ1c3P+y6L+17jmwYvGxsj+ehXLysuPj5X+kRv+giL9hSj+ii/9izP+fhz9mSv+nTOBf4DDmlwiHiDp2cPt4M7eqF7u49LJpWzcxqLOrHn+69U1MTL+mUt+gI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wUqNg/Oh4ZECn1b2JjhgqCLxBjYGRKraHx8ZEQIX4Oq74MYab/eIB9WGsuLJDSS0kCXZUnPTvQDC4dN8gAc52HqStfxugEy1zih7APzgLIcY2UGvxDPOcCnVmY410Li9lUF3bVYMlhGebTMUZJYHgE+WDXQL1bCjjfohj5/kZIyWI0RJWHnTWTyNbVr5XzIhU3cI+jxHkmvGIbTU0BlDO3ri6GOCp5cruDg0flB0kDllGNcnShZDPe2TFvrgABIXh2HayzX32ScG0ofWclEY2MH9V2mtuql9YzGQx6aiHO+uK1klFZzii8WysnJyMTi0+dNT7NZPBgEXMtkzk0cdRq8iAyuVBRDY+2M5lMzvY0mMraw9jYZLy2y1s3MkyHhqsSNGSbVW0ry+qaQ+Z8+SQSTeQVw9U5611tzFDm07m6+6Oud38B9w7WHaw7WHew7vQ/BIXvQIBo140AAAAASUVORK5CYII="
var decoded = atob(image.split(",")[1])
  • That worked, and now I have something like: PNG then a lot of empty spaces, and then a lot of characters I can't seem to paste here, but it still won't load, and says it's an invalid image. – TrySpace Apr 13 '16 at 10:22
  • I think I maybe should not put the decoded string as text inside the `input` as `value`? But I don't know any other way to get this form to upload it. – TrySpace Apr 13 '16 at 11:00
  • When I compare the converted binary/hex it has a very different structure than a working image... – TrySpace Apr 13 '16 at 12:14
  • So when using this: http://decodebase64.com/ to decode a `base64` and just plain copy paste that into the file, it won't display. I am missing something essential here, but I have no idea where to look... – TrySpace Apr 13 '16 at 12:40
  • I guess you kindof answered half of the question, at least now I can convert it. But I still can't use that to generate a file. So, I've made a more specific question: https://stackoverflow.com/questions/36599064/convert-base64-to-file-to-upload-in-forminput-name-file – TrySpace Apr 13 '16 at 13:01