You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ImageKit Javascript SDK allows you to use real-time [image resizing](https://docs.imagekit.io/features/image-transformations), [optimization](https://docs.imagekit.io/features/image-optimization), and [file uploading](https://docs.imagekit.io/api-reference/upload-file-api/client-side-file-upload) in the client-side.
13
+
Javascript SDK for [ImageKit](https://imagekit.io/) provides URL generation for image & video resizing and provides an interface for file upload. This SDK is lightweight and you can also use this as an ES module.
14
14
15
-
This SDK is lightweight and has no dependency. You can also use this as an ES module.
15
+
ImageKit is complete media storage, optimization, and transformation solution that comes with an [image and video CDN](https://imagekit.io/features/imagekit-infrastructure). It can be integrated with your existing infrastructure - storage like AWS S3, web servers, your CDN, and custom domain names, allowing you to deliver optimized images in minutes with minimal code changes.
16
16
17
17
## Installation
18
18
@@ -85,6 +85,7 @@ Create a file `.env` using `sample.env` in the directory `samples/sample-app` an
85
85
Now start the sample application by running:
86
86
87
87
```
88
+
// Run it from project root
88
89
yarn startSampleApp
89
90
```
90
91
@@ -95,7 +96,7 @@ You can use this SDK for URL generation and client-side file uploads.
95
96
96
97
**1. Using image path and image hostname or endpoint**
97
98
98
-
This method allows you to create a URL using the `path` where the image exists and the URL endpoint (`urlEndpoint`) you want to use to access the image. You can refer to the documentation [here](https://docs.imagekit.io/integration/url-endpoints) to read more about URL endpoints in ImageKit and the section about [image origins](https://docs.imagekit.io/integration/configure-origin) to understand about paths with different kinds of origins.
99
+
This method allows you to create an URL to access a file using the relative file path and the ImageKit URL endpoint (`urlEndpoint`). The file can be an image, video, or any other static file supported by ImageKit.
This method allows you to add transformation parameters to an existing, complete URL that is already mapped to ImageKit using the `src` parameter. This method should be used if you have the complete URL mapped to ImageKit stored in your database.
120
+
This method allows you to add transformation parameters to an absolute URL. For example, if you have configured a custom CNAME and have absolute asset URLs in your database or CMS, you will often need this.
120
121
121
122
122
123
```
@@ -141,11 +142,11 @@ The `.url()` method accepts the following parameters
| urlEndpoint | Optional. The base URL to be appended before the path of the image. If not specified, the URL Endpoint specified at the time of SDK initialization is used. For example, https://ik.imagekit.io/your_imagekit_id/endpoint/|
144
-
| path | Conditional. This is the path at which the image exists. For example, `/path/to/image.jpg`. Either the `path` or `src` parameter need to be specified for URL generation. |
145
-
| src | Conditional. This is the complete URL of an image already mapped to ImageKit. For example, `https://ik.imagekit.io/your_imagekit_id/endpoint/path/to/image.jpg`. Either the `path` or `src` parameter need to be specified for URL generation. |
146
-
| transformation | Optional. An array of objects specifying the transformation to be applied in the URL. The transformation name and the value should be specified as a key-value pair in the object. Different steps of a [chained transformation](https://docs.imagekit.io/features/image-transformations/chained-transformations) can be specified as different objects of the array. The complete list of supported transformations in the SDK and some examples of using them are given later. If you use a transformation name that is not specified in the SDK, it gets applied as it is in the URL. |
147
-
| transformationPostion | Optional. The default value is `path` that places the transformation string as a path parameter in the URL. It can also be specified as `query` which adds the transformation string as the query parameter `tr` in the URL. If you use `src` parameter to create the URL, then the transformation string is always added as a query parameter. |
148
-
| queryParameters | Optional. These are the other query parameters that you want to add to the final URL. These can be any query parameters and not necessarily related to ImageKit. Especially useful if you want to add some versioning parameter to your URLs. |
145
+
| path | Conditional. This is the path at which the image exists. For example, `/path/to/image.jpg`. Either the `path` or `src` parameter needs to be specified for URL generation. |
146
+
| src | Conditional. This is the complete URL of an image already mapped to ImageKit. For example, `https://ik.imagekit.io/your_imagekit_id/endpoint/path/to/image.jpg`. Either the `path` or `src` parameter needs to be specified for URL generation. |
147
+
| transformation | Optional. An array of objects specifying the transformation to be applied in the URL. The transformation name and the value should be specified as a key-value pair in the object. Different steps of a [chained transformation](https://docs.imagekit.io/features/image-transformations/chained-transformations) can be specified as different objects of the array. The complete list of supported transformations in the SDK and some examples of using them are given later. If you use a transformation name that is not specified in the SDK, it gets applied as it is in the URL. |
148
+
| transformationPostion | Optional. The default value is `path`, which places the transformation string as a path parameter in the URL. It can also be specified as `query`, which adds the transformation string as the query parameter `tr` in the URL. If you use the`src` parameter to create the URL, then the transformation string is always added as a query parameter. |
149
+
| queryParameters | Optional. These are the other query parameters that you want to add to the final URL. These can be any query parameters and are not necessarily related to ImageKit. Especially useful if you want to add some versioning parameters to your URLs. |
The complete list of transformations supported and their usage in ImageKit can be found [here](https://docs.imagekit.io/features/image-transformations). The SDK gives a name to each transformation parameter, making the code simpler and readable. If a transformation is supported in ImageKit, but a name for it cannot be found in the table below, then use the transformation code from ImageKit docs as the name when using in the `url` function.
193
+
See the complete list of transformations supported in ImageKit [here](https://docs.imagekit.io/features/image-transformations). The SDK gives a name to each transformation parameter e.g. `height` for `h` and `width`for `w` parameter. It makes your code more readable. If the property does not match any of the following supported options, it is added as it is.
193
194
194
195
If you want to generate transformations in your application and add them to the URL as it is, use the `raw` parameter.
195
196
@@ -256,18 +257,19 @@ If you want to generate transformations in your application and add them to the
256
257
257
258
### File Upload
258
259
259
-
The SDK provides a simple interface using the `.upload()` method to upload files to the ImageKit Media Library. It accepts all the parameters supported by the [ImageKit Upload API](https://docs.imagekit.io/api-reference/upload-file-api/client-side-file-upload).
260
+
The SDK provides a simple interface using the `.upload()` method to upload files to the ImageKit Media Library.
260
261
261
-
The `upload()` method requires `file` and the `fileName` parameter.
262
+
The `upload()` method requires mandatory `file` and the `fileName` parameter. In addition, it accepts all the parameters supported by the [ImageKit Upload API](https://docs.imagekit.io/api-reference/upload-file-api/client-side-file-upload).
262
263
263
264
Also, make sure that you have specified `authenticationEndpoint` during SDK initialization. The SDK makes an HTTP GET request to this endpoint and expects a JSON response with three fields, i.e. `signature`, `token`, and `expire`.
264
265
265
266
[Learn how to implement authenticationEndpoint](https://docs.imagekit.io/api-reference/upload-file-api/client-side-file-upload#how-to-implement-authenticationendpoint-endpoint) on your server.
266
267
267
268
You can pass other parameters supported by the ImageKit upload API using the same parameter name as specified in the upload API documentation. For example, to specify tags for a file at the time of upload, use the `tags` parameter as specified in the [documentation here](https://docs.imagekit.io/api-reference/upload-file-api/client-side-file-upload).
268
269
269
-
Sample usage
270
-
```
270
+
271
+
#### Sample usage
272
+
```html
271
273
<formaction="#"onsubmit="upload()">
272
274
<inputtype="file"id="file1" />
273
275
<inputtype="submit" />
@@ -287,6 +289,8 @@ Sample usage
287
289
// Upload function internally uses the ImageKit.io javascript SDK
288
290
functionupload(data) {
289
291
var file =document.getElementById("file1");
292
+
293
+
// Using Callback Function
290
294
imagekit.upload({
291
295
file:file.files[0],
292
296
fileName:"abc1.jpg",
@@ -299,11 +303,25 @@ Sample usage
299
303
}
300
304
]
301
305
}, function(err, result) {
302
-
console.log(arguments);
303
-
console.log(imagekit.url({
304
-
src: result.url,
305
-
transformation: [{ height: 300, width: 400}]
306
-
}));
306
+
console.log(result);
307
+
})
308
+
309
+
// Using Promises
310
+
imagekit.upload({
311
+
file:file.files[0],
312
+
fileName:"abc1.jpg",
313
+
tags: ["tag1"],
314
+
extensions: [
315
+
{
316
+
name:"aws-auto-tagging",
317
+
minConfidence:80,
318
+
maxTags:10
319
+
}
320
+
]
321
+
}).then(result=> {
322
+
console.log(result);
323
+
}).then(error=> {
324
+
console.log(error);
307
325
})
308
326
}
309
327
</script>
@@ -312,3 +330,81 @@ Sample usage
312
330
If the upload succeeds, `err` will be `null`, and the `result` will be the same as what is received from ImageKit's servers.
313
331
If the upload fails, `err` will be the same as what is received from ImageKit's servers, and the `result` will be null.
314
332
333
+
## Tracking upload progress using custom XMLHttpRequest
334
+
You can use a custom XMLHttpRequest object as the following to bind `progress` or any other events for a customized implementation.
335
+
336
+
```js
337
+
var fileSize =file.files[0].size;
338
+
var customXHR =newXMLHttpRequest();
339
+
customXHR.upload.addEventListener('progress', function (e) {
340
+
if (e.loaded<= fileSize) {
341
+
var percent =Math.round(e.loaded/ fileSize *100);
342
+
console.log(`Uploaded ${percent}%`);
343
+
}
344
+
345
+
if(e.loaded==e.total){
346
+
console.log("Upload done");
347
+
}
348
+
});
349
+
350
+
imagekit.upload({
351
+
xhr: customXHR,
352
+
file:file.files[0],
353
+
fileName:"abc1.jpg",
354
+
tags: ["tag1"],
355
+
extensions: [
356
+
{
357
+
name:"aws-auto-tagging",
358
+
minConfidence:80,
359
+
maxTags:10
360
+
}
361
+
]
362
+
}).then(result=> {
363
+
console.log(result);
364
+
}).then(error=> {
365
+
console.log(error);
366
+
})
367
+
```
368
+
369
+
## Access request-id, other response headers, and HTTP status code
370
+
You can access `$ResponseMetadata` on success or error object to access the HTTP status code and response headers.
Copy file name to clipboardExpand all lines: src/constants/errorMessages.ts
+2-1
Original file line number
Diff line number
Diff line change
@@ -6,9 +6,10 @@ export default {
6
6
MISSING_UPLOAD_FILE_PARAMETER: {message: "Missing file parameter for upload",help: ""},
7
7
MISSING_UPLOAD_FILENAME_PARAMETER: {message: "Missing fileName parameter for upload",help: ""},
8
8
MISSING_AUTHENTICATION_ENDPOINT: {message: "Missing authentication endpoint for upload",help: ""},
9
-
MISSING_PUBLIC_KEY: {message: "Missing public key for upload",help: ""},
9
+
MISSING_PUBLIC_KEY: {message: "Missing public key for upload",help: ""},
10
10
AUTH_ENDPOINT_TIMEOUT: {message: "The authenticationEndpoint you provided timed out in 60 seconds",help: ""},
11
11
AUTH_ENDPOINT_NETWORK_ERROR: {message: "Request to authenticationEndpoint failed due to network error",help: ""},
12
+
AUTH_INVALID_RESPONSE: {message: "Invalid response from authenticationEndpoint. The SDK expects a JSON response with three fields i.e. signature, token and expire.",help: ""},
12
13
UPLOAD_ENDPOINT_NETWORK_ERROR: {
13
14
message: "Request to ImageKit upload endpoint failed due to network error",
0 commit comments