Uploading a png to imgur using javascript
Asked Answered
K

2

5

I'm trying to use Javascript to upload a png to imgur. I've used the code directly from the Imgur API example, but I don't think I am passing the png file properly as I get an error message saying file.type is undefined. I think the file is ok as I've tried this with a few different pngs. My code is as follows:

<html>
<head>
<script type="text/javascript">
function upload(file) {
   // file is from a <input> tag or from Drag'n Drop
   // Is the file an image?
   if (!file || !file.type.match(/image.*/)) return;

   // It is!
   // Let's build a FormData object
   var fd = new FormData();
   fd.append("image", file); // Append the file
   fd.append("key", "mykey"); // Get your own key: http://api.imgur.com/

   // Create the XHR (Cross-Domain XHR FTW!!!)
   var xhr = new XMLHttpRequest();
   xhr.open("POST", "http://api.imgur.com/2/upload.json"); // Boooom!
   xhr.onload = function() {
      // Big win!
      // The URL of the image is:
      JSON.parse(xhr.responseText).upload.links.imgur_page;
   }

   // Ok, I don't handle the errors. An exercice for the reader.
   // And now, we send the formdata
   xhr.send(fd);
}
</script>
</head>   

<body>

<button type="button" onclick="upload('test.png')">upload to imgur</button> 

</body>
</html> 

The png file test.png is stored in the same directory as my html file.

Kinase answered 12/2, 2012 at 14:46 Comment(0)
N
2

The example you are using expects that you use input element (type=file) to upload some image. Try this example. You can access image data using a Canvas like this.

To summarize, you'll need to do this:

  1. Create new Image with your file (needs to be on local domain)
  2. Draw the image to a Canvas at onload
  3. Extract image data using this method
  4. Pass that data to imgur like here
Nomism answered 12/2, 2012 at 14:54 Comment(3)
Thanks for the suggestions. For adding an image to the canvas, is that the image id? I assume I can't refer to it using the filename, based on @skimberk1's comment.Kinase
There's no way to refer to the image using filename. You'll need to load it to an img element and then load its contents to a canvas using drawImage. It is important to do this latter part at the img's onload handler. Otherwise it won't work.Epanodos
Thanks bebraw, that helps me understand the flow. I'm experimenting with html2canvas.hertzen.com at the moment; it looks like it will do part of these steps.Kinase
P
3

The file has to be a File object created with the HTML5 fileAPI, you can't just give it a file name and expect it to work.

Javascript has no access to the filesystem, it can only access files that are given to it using either drag and drop or a file input.

Pressure answered 12/2, 2012 at 14:49 Comment(0)
N
2

The example you are using expects that you use input element (type=file) to upload some image. Try this example. You can access image data using a Canvas like this.

To summarize, you'll need to do this:

  1. Create new Image with your file (needs to be on local domain)
  2. Draw the image to a Canvas at onload
  3. Extract image data using this method
  4. Pass that data to imgur like here
Nomism answered 12/2, 2012 at 14:54 Comment(3)
Thanks for the suggestions. For adding an image to the canvas, is that the image id? I assume I can't refer to it using the filename, based on @skimberk1's comment.Kinase
There's no way to refer to the image using filename. You'll need to load it to an img element and then load its contents to a canvas using drawImage. It is important to do this latter part at the img's onload handler. Otherwise it won't work.Epanodos
Thanks bebraw, that helps me understand the flow. I'm experimenting with html2canvas.hertzen.com at the moment; it looks like it will do part of these steps.Kinase

© 2022 - 2024 — McMap. All rights reserved.