@@ -287,7 +287,9 @@ Sample usage
287287 // Upload function internally uses the ImageKit.io javascript SDK
288288 function upload(data) {
289289 var file = document.getElementById("file1");
290- imagekit.upload({
290+
291+ // Using Callback Function
292+ var xhr = imagekit.upload({
291293 file: file.files[0],
292294 fileName: "abc1.jpg",
293295 tags: ["tag1"],
@@ -305,10 +307,78 @@ Sample usage
305307 transformation: [{ height: 300, width: 400}]
306308 }));
307309 })
310+
311+ xhr.upload.onprogress = (event) => {
312+ console.log(`Uploaded ${event.loaded} of ${event.total} bytes`);
313+ }
314+
315+ // Using Promises
316+ imagekit.upload({
317+ file: file.files[0],
318+ fileName: "abc1.jpg",
319+ tags: ["tag1"],
320+ extensions: [
321+ {
322+ name: "aws-auto-tagging",
323+ minConfidence: 80,
324+ maxTags: 10
325+ }
326+ ]
327+ }).then(result => {
328+ console.log(imagekit.url({
329+ src: result.url,
330+ transformation: [{ height: 300, width: 400}]
331+ }));
332+ }).then(error => {
333+ console.log(error);
334+ })
308335 }
309336</script>
310337```
311338
312339If the upload succeeds, ` err ` will be ` null ` , and the ` result ` will be the same as what is received from ImageKit's servers.
313340If the upload fails, ` err ` will be the same as what is received from ImageKit's servers, and the ` result ` will be null.
341+ Upload using callback functions returns the upload XHR, it can be used to monitor the progress of the upload.
342+
343+ ## Access request-id, other response headers and HTTP status code
344+ You can access ` $ResponseMetadata ` on success or error object to access the HTTP status code and response headers.
345+
346+ ``` javascript
347+ // Success
348+ var response = await imagekit .upload ({
349+ file: file .files [0 ],
350+ fileName: " abc1.jpg" ,
351+ tags: [" tag1" ],
352+ extensions: [
353+ {
354+ name: " aws-auto-tagging" ,
355+ minConfidence: 80 ,
356+ maxTags: 10
357+ }
358+ ]
359+ });
360+ console .log (response .$ResponseMetadata .statusCode ); // 200
361+
362+ // { 'content-length': "300", 'content-type': 'application/json', 'x-request-id': 'ee560df4-d44f-455e-a48e-29dfda49aec5'}
363+ console .log (response .$ResponseMetadata .headers );
364+
365+ // Error
366+ try {
367+ await imagekit .upload ({
368+ file: file .files [0 ],
369+ fileName: " abc1.jpg" ,
370+ tags: [" tag1" ],
371+ extensions: [
372+ {
373+ name: " aws-auto-tagging" ,
374+ minConfidence: 80 ,
375+ maxTags: 10
376+ }
377+ ]
378+ });
379+ } catch (ex) {
380+ console .log (response .$ResponseMetadata .statusCode ); // 200
314381
382+ // {'content-type': 'application/json', 'x-request-id': 'ee560df4-d44f-455e-a48e-29dfda49aec5'}
383+ console .log (response .$ResponseMetadata .headers );
384+ }
0 commit comments