|
1 | | -# react-file-upload |
| 1 | +# File Management with Preview |
| 2 | + |
| 3 | +A versatile and user-friendly file management system built with React, Next.js and TypeScript that allows for single and multiple file uploading with a preview feature. It allows you to select files and preview them, returning an array of selected files. You can provide custom design and override classes for your file component. |
| 4 | + |
| 5 | +Checkout the live demo on, <br /> |
| 6 | +[](https://codesandbox.io/p/sandbox/cranky-breeze-r4hht7?file=%2Fsrc%2Fmain.js) |
| 7 | + |
| 8 | +## Table of Contents |
| 9 | + |
| 10 | +- [Features](#features) |
| 11 | +- [Getting Started](#getting-started) |
| 12 | + - [Prerequisites](#prerequisites) |
| 13 | + - [Installation](#installation) |
| 14 | +- [Usage](#usage) |
| 15 | +- [Examples](#examples) |
| 16 | + - [Square View](#square-view) |
| 17 | + - [Horizontal Long Square View](#horizontal-long-square-view) |
| 18 | + - [Circular View](#circular-view) |
| 19 | + - [Over-ride CSS](#over-ride-css) |
| 20 | +- [Contributing](#contributing) |
| 21 | +- [License](#license) |
| 22 | +- [Contact Information](#contact-information) |
| 23 | + |
| 24 | +## Features |
| 25 | + |
| 26 | +- **Single File Upload:** Users can upload or change a single file. |
| 27 | +- **Multiple File Management:** Easily manage multiple files. |
| 28 | +- **File Preview:** Provides a preview of uploaded files (e.g., images, videos, gifs). |
| 29 | +- **Responsive Design:** Ensures a seamless experience on various devices. |
| 30 | + |
| 31 | +## Getting Started |
| 32 | + |
| 33 | +Follow these instructions to set up and run the project on your local machine. |
| 34 | + |
| 35 | +### Prerequisites |
| 36 | + |
| 37 | +Before you begin, make sure you have the following software installed: |
| 38 | + |
| 39 | +- Node.js: [https://nodejs.org/](https://nodejs.org/) |
| 40 | +- A [Next.js](https://nextjs.org/) project bootstrapped with [create-next-app](https://github.com/vercel/next.js/tree/canary/packages/create-next-app). |
| 41 | + |
| 42 | +``` |
| 43 | +> npm install -D sass |
| 44 | +``` |
| 45 | + |
| 46 | +### Installation |
| 47 | + |
| 48 | +To use this library, you can install it via npm: |
| 49 | + |
| 50 | +``` |
| 51 | +> npm install @canopassoftware/vue-file-upload |
| 52 | +``` |
| 53 | + |
| 54 | +Make sure to check the library's documentation for any additional setup or configuration steps. |
| 55 | + |
| 56 | +## Usage |
| 57 | + |
| 58 | +To manage and preview files with this library, follow these steps: |
| 59 | + |
| 60 | +### Import the library into your file |
| 61 | + |
| 62 | +```js |
| 63 | +// for CommonJS |
| 64 | +const { SingleFileUpload, MultipleFileUpload } = require('@canopassoftware/vue-file-upload') |
| 65 | + |
| 66 | +OR |
| 67 | +// for esModules |
| 68 | +import { SingleFileUpload, MultipleFileUpload } from '@canopassoftware/vue-file-upload' |
| 69 | +``` |
| 70 | + |
| 71 | +Now, you can use that imported component with adding your custom UI for file uploading. Here are some examples of how to use the imported component: |
| 72 | + |
| 73 | +### Single File Upload Management |
| 74 | + |
| 75 | +```js |
| 76 | +import Image from "next/image"; |
| 77 | +import React, { useState } from "react"; |
| 78 | +import SingleFileUpload from "./singleFile"; |
| 79 | + |
| 80 | +export default function App() { |
| 81 | + const [fileData, setPreviewFileData] = useState( |
| 82 | + {} as { |
| 83 | + previewType: string; |
| 84 | + previewUrl: string | ArrayBuffer | null; |
| 85 | + previewName: string; |
| 86 | + isDragging: boolean; |
| 87 | + isLoading: boolean; |
| 88 | + } |
| 89 | + ); |
| 90 | + |
| 91 | + const getPreviewFileData = (file: any) => { |
| 92 | + setPreviewFileData(file); |
| 93 | + }; |
| 94 | + |
| 95 | + const [uploadingStatus, setUploadingData] = useState(false); |
| 96 | + |
| 97 | + const isUploading = (flag: boolean) => { |
| 98 | + setUploadingData(flag); |
| 99 | + |
| 100 | + if (flag) { |
| 101 | + setTimeout(() => { |
| 102 | + setUploadingData(!flag); |
| 103 | + }, 5000); |
| 104 | + } |
| 105 | + }; |
| 106 | + |
| 107 | + const [getFile, setFileData] = useState({}); |
| 108 | + |
| 109 | + const getFileData = (file: any) => { |
| 110 | + setFileData(file); |
| 111 | + }; |
| 112 | +} |
| 113 | +``` |
| 114 | + |
| 115 | +### Description |
| 116 | + |
| 117 | +- In the below html code we provided default design for file management, you can modify this design according to your requirements. |
| 118 | +- The `getPreview` prop containing `file` object with keys `previewType`, `previewUrl`, `previewName`, and `isDragging` for showing preview. like, |
| 119 | + |
| 120 | +```sh |
| 121 | +file: { |
| 122 | + previewType: 'video', |
| 123 | + previewUrl: 'data:image/jpeg;base64,/9j/D1AAAACRsdW1pAAAD...', |
| 124 | + previewName: 'a152148640581.62d918f12a0b4.mp4', |
| 125 | + isDragging: false |
| 126 | +} |
| 127 | +``` |
| 128 | + |
| 129 | +- previewType: - Type of the preview. like, file is image or video |
| 130 | +- previewUrl: - URL of the file preview |
| 131 | +- previewName: - Preview file name |
| 132 | +- isDragging: - You will get it true when you dragging the file on your design |
| 133 | + |
| 134 | +- You need to manage it by calling a function and get value of that object in variable as we have defined above by using `getPreviewFileData()`. |
| 135 | + |
| 136 | +```html |
| 137 | +return ( |
| 138 | + <main className="flex min-h-screen flex-col justify-between p-5"> |
| 139 | + <SingleFileUpload |
| 140 | + getPreview={getPreviewFileData} |
| 141 | + getFileObj={getFileData} |
| 142 | + isUploading={isUploading} |
| 143 | + uploadingStatus={uploadingStatus} |
| 144 | + uploadBtn={"Save"} |
| 145 | + progressBtn={"Saving..."} |
| 146 | + > |
| 147 | + <div className="m-5"> |
| 148 | + <div className="flex items-center justify-center"> |
| 149 | + {!fileData || !fileData.previewUrl ? ( |
| 150 | + <label className="flex flex-col items-center justify-center w-full h-56 border-2 border-gray-300 border-dashed rounded-lg cursor-pointer bg-gray-50 dark:hover:bg-bray-800 dark:bg-gray-700 hover:bg-gray-100 dark:border-gray-600 dark:hover:border-gray-500 dark:hover:bg-gray-600"> |
| 151 | + <div className="flex flex-col items-center justify-center pt-5 pb-6 px-10"> |
| 152 | + <svg |
| 153 | + className="w-8 h-8 mb-4 text-gray-500 dark:text-gray-400" |
| 154 | + aria-hidden="true" |
| 155 | + xmlns="http://www.w3.org/2000/svg" |
| 156 | + fill="none" |
| 157 | + viewBox="0 0 20 16" |
| 158 | + > |
| 159 | + <path |
| 160 | + stroke="currentColor" |
| 161 | + strokeLinecap="round" |
| 162 | + strokeLinejoin="round" |
| 163 | + strokeWidth="2" |
| 164 | + d="M13 13h3a3 3 0 0 0 0-6h-.025A5.56 5.56 0 0 0 16 6.5 5.5 5.5 0 0 0 5.207 5.021C5.137 5.017 5.071 5 5 5a4 4 0 0 0 0 8h2.167M10 15V6m0 0L8 8m2-2 2 2" |
| 165 | + /> |
| 166 | + </svg> |
| 167 | + <p className="mb-2 text-sm text-gray-500 dark:text-gray-400"> |
| 168 | + <span className="font-semibold">Click to upload</span> or |
| 169 | + drag and drop |
| 170 | + </p> |
| 171 | + <p className="text-xs text-gray-500 dark:text-gray-400"> |
| 172 | + SVG, PNG, JPG or GIF |
| 173 | + </p> |
| 174 | + </div> |
| 175 | + </label> |
| 176 | + ) : ( |
| 177 | + <div className="flex items-center justify-center"> |
| 178 | + {fileData.previewType != "video" ? ( |
| 179 | + <Image |
| 180 | + className="object-contain rounded-2xl w-72 h-56" |
| 181 | + src={fileData.previewUrl as string} |
| 182 | + height={224} |
| 183 | + width={300} |
| 184 | + alt="image" |
| 185 | + /> |
| 186 | + ) : ( |
| 187 | + <video |
| 188 | + autoPlay |
| 189 | + loop |
| 190 | + className="h-64 w-72 object-contain rounded-2xl" |
| 191 | + > |
| 192 | + <source |
| 193 | + src={fileData.previewUrl as string} |
| 194 | + type="video/mp4" |
| 195 | + /> |
| 196 | + </video> |
| 197 | + )} |
| 198 | + </div> |
| 199 | + )} |
| 200 | + </div> |
| 201 | + <p className="flex items-center justify-center text-center"> |
| 202 | + {fileData ? fileData.previewName : ""} |
| 203 | + </p> |
| 204 | + </div> |
| 205 | + </SingleFileUpload> |
| 206 | + </main> |
| 207 | + ); |
| 208 | +``` |
| 209 | + |
| 210 | +<img src="./src/assets/images/single-file-uploading.gif"/> |
| 211 | + |
| 212 | +- Also, you can over-ride all the classes for changing the UI as per your requirement. |
| 213 | + |
| 214 | +## Examples |
| 215 | + |
| 216 | +We are providing some examples with design. so, you can easily take it and use it in your project. |
| 217 | + |
| 218 | +### Over-ride CSS |
| 219 | + |
| 220 | +For over-riding the design of default buttons, you can over-ride it's CSS by class name. <br> |
| 221 | +For example., <br> |
| 222 | + |
| 223 | +- Over-ride CSS of remove file button you can add it like, |
| 224 | + |
| 225 | +```css |
| 226 | +.remove-btn { |
| 227 | + color: white; |
| 228 | + background-color: red; |
| 229 | + font-size: 25px; |
| 230 | + padding: 5px; |
| 231 | +} |
| 232 | +``` |
| 233 | + |
| 234 | +- Over-ride CSS of submit/upload file button you can add it like, |
| 235 | + |
| 236 | +```css |
| 237 | +.upload-btn { |
| 238 | + color: white; |
| 239 | + background-color: rgb(51, 65, 85); |
| 240 | + font-size: 25px; |
| 241 | + padding: 5px 10px; |
| 242 | +} |
| 243 | +``` |
| 244 | + |
| 245 | +## Contributing |
| 246 | + |
| 247 | +We welcome contributions from the community. To contribute to this project, please follow these guidelines: |
| 248 | + |
| 249 | +- Fork the repository. |
| 250 | +- Create a new branch for your feature or bug fix. |
| 251 | +- Make your changes and commit them. |
| 252 | +- Push your changes to your fork. |
| 253 | +- Submit a pull request with a clear description of your changes. |
| 254 | +- Please ensure your code follows the project's coding standards and includes appropriate documentation. |
| 255 | + |
| 256 | +## License |
| 257 | + |
| 258 | +This project is licensed under the MIT. |
| 259 | + |
| 260 | +## Contact Information |
| 261 | + |
| 262 | +If you have questions or need support, you can reach out to us at GitHub Profile. |
0 commit comments