Created
March 10, 2023 07:38
-
-
Save Mirochiu/4d7eb441e2fed8a1aa77467f26117c42 to your computer and use it in GitHub Desktop.
upload image to Imgur with Client ID
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="zh-Hant-TW"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <script src="http://code.jquery.com/jquery.min.js"></script> | |
| </head> | |
| <body> | |
| <input id="imgurClientId" type="text"> | |
| <form> | |
| <input type="file" name="image" accept=".jpg,.jpeg,.png,.gif,.bmp,.webp"> | |
| <button type="submit">upload</button> | |
| <input name="type" type="hidden" value="file"> | |
| </form> | |
| <pre></pre> | |
| <script> | |
| $(() => { | |
| console.log('onready') | |
| $('form').on('submit', (e) => { | |
| e.preventDefault(); | |
| // https://apidocs.imgur.com/#c85c9dfc-7487-4de2-9ecd-66f727cf3139 | |
| $.ajax({ | |
| type: 'POST', | |
| url: 'https://api.imgur.com/3/upload', | |
| headers: { | |
| Authorization: 'Client-ID ' + $('#imgurClientId').val(), | |
| }, | |
| crossDomain: true, | |
| contentType: false, | |
| mimeType: 'multipart/form-data', | |
| processData: false, | |
| data: new FormData(e.target), | |
| dataType: 'json', | |
| success: (result) => { | |
| console.log(result) | |
| $('pre').text(JSON.stringify(result, null, ' ')) | |
| }, | |
| error: (xhr, txt, thrown) => { | |
| console.error(xhr, txt, thrown) | |
| $('pre').text(txt) | |
| } | |
| }); | |
| }) | |
| }); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment