Upload Files
Uploading Files with ExaDrive SDK
The ExaDrive SDK provides a straightforward method for uploading files to the ExaDrive network. This functionality allows you to easily integrate file uploads into your application, with the added benefit of receiving detailed information about the uploaded file.
Basic Usage
To upload a file, use the uploadFile
method of your ExaDrive instance. This method takes the file path as an argument and returns a promise that resolves when the upload is complete.
Syntax
filePath
: A string representing the path to the file you want to upload.directoryPath
: A string which will represent the directory you want to put your file in.
Example
Response Structure
Upon successful upload, the promise resolves with a response object containing detailed information about the uploaded file.
Sample Response
Response Fields
filename: The name of the uploaded file.
fileDetails: An object containing technical details about the file:
encoding: The encoding type of the file (e.g., "7bit").
mimetype: The MIME type of the file (e.g., "image/png").
size: The size of the file in bytes.
url: The URL where the uploaded file can be accessed.
Advanced Usage
Batch Uploads
If you need to upload multiple files, you can use Promise.all():
Best Practices
Error Handling: Always include error handling to manage potential upload failures gracefully.
File Size Limits: Be aware of any file size limits imposed by ExaDrive and handle them appropriately in your application.
File Type Validation: Implement client-side file type validation before uploading to ensure only permitted file types are sent.
Secure File Handling: Ensure that file paths are properly sanitized and validated to prevent security vulnerabilities.
Retry Mechanism: For improved reliability, especially with large files or unstable connections, implement a retry mechanism:
uploadMulterFile
uploadMulterFile
Using this function you can directly use Multer file object to upload a file. This is an alternative to uploadFile.
Parameters:
file
: The request object containing the file data.virtualDirectoryPath
: The path you want to set for the directory of the file.
Sample Response
uploadFileWithBuffer
uploadFileWithBuffer
The uploadFileWithBuffer
function allows you to upload files using a buffer. This is useful when you have file data in memory and want to upload it directly without saving it to disk first.
Parameters:
buffer
: The buffer containing the file data.originalFileName
: The original name of the file, this will help in locating the file on ExaDrive later.mimeType
: The mime-type of the file.virtualDirectoryPath
: The path you want to set for the directory of the file.
Sample Response
By leveraging these upload capabilities and following best practices, you can efficiently manage file uploads in your application using the ExaDrive SDK.
Last updated