Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <main uk-height-viewport="expand: true">
	<div class="uk-container uk-margin-top uk-margin-bottom">
		<p class="uk-text-lead">Multiple file upload with image preview</p>
		<div class="uk-upload-box">
			<div id="error-alert" class="uk-alert-danger uk-margin-top uk-hidden" uk-alert>
				<p id="error-messages"></p>
			</div>

			<div class="drop__zone uk-placeholder uk-text-center">
				<span uk-icon="icon: cloud-upload"></span>
				<span class="uk-text-middle uk-margin-small-left">Attach files by dropping them here or</span>
				<div uk-form-custom>
					<input name="documents[]" type="file" accept="image/png, image/jpeg, application/pdf, application/vnd.openxmlformats-officedocument.wordprocessingml.document" multiple>
					<span class="uk-link">selecting one</span>
				</div>

				<ul id="preview" class="uk-list uk-grid-match uk-child-width-1-2 uk-child-width-1-4@l uk-child-width-1-5@xl uk-text-center" uk-grid uk-scrollspy="cls: uk-animation-scale-up; target: .list-item; delay: 80"></ul>
			</div>
		</div>

		<hr>
		<p class="uk-text-lead">Single file upload image preview</p>

		<div class="uk-upload-box">
			<div id="error-alert" class="uk-alert-danger uk-margin-top uk-hidden" uk-alert>
				<p id="error-messages"></p>
			</div>

			<div class="drop__zone uk-placeholder uk-text-center">
				<span uk-icon="icon: cloud-upload"></span>
				<span class="uk-text-middle uk-margin-small-left">Attach files by dropping them here or</span>
				<div uk-form-custom>
					<input name="document" accept="image/png, image/jpeg, application/pdf, application/vnd.openxmlformats-officedocument.wordprocessingml.document" type="file">
					<span class="uk-link">selecting one</span>
				</div>

				<ul id="preview" class="uk-list uk-grid-match uk-child-width-1-2 uk-child-width-1-4@l uk-child-width-1-5@xl uk-text-center" uk-grid uk-scrollspy="cls: uk-animation-scale-up; target: .list-item; delay: 80"></ul>
			</div>
		</div>

		<!-- 	Random Card	 -->

		<div class="uk-card uk-card-hover uk-box-shadow-medium" hidden>
			<div class="uk-card-header uk-background-primary uk-light">
				<h3 class="uk-heading uk-heading-bullet uk-card-title">I am a card title</h3>
			</div>
			<div class="uk-card-body">
				<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Dolores asperiores totam recusandae aliquam nulla velit suscipit necessitatibus facere. Alias exercitationem sit magni. Inventore accusantium minima illum cupiditate saepe non laborum, voluptatibus accusamus ducimus fugiat tempore voluptatem a tempora, omnis voluptatum.</p>
			</div>
			<div class="uk-card-footer uk-background-muted">
				<span class="uk-text-meta uk-text-muted">I am a card footer</span>
			</div>
		</div>
	</div>
</main>

<!-- FOOTER || ALWAYS STICKS TO BOTTOM ;-)  -->
<footer>
	<div class="uk-section uk-section-xsmall uk-margin-small-top uk-section-muted">
		<div class="uk-container uk-container-expand">
			<div class="uk-grid uk-child-width-1-2@s uk-text-center uk-text-left@s" data-uk-grid>
				<div class="uk-text-small uk-text-muted">
					&copy; 2022 Your Company | All rights reserved.
				</div>
				<div class="uk-text-small uk-text-muted uk-text-center uk-text-right@s">
					<span>Designed & Developed with <i class="mdi mdi-heart" style="color: palevioletred"></i> By </span> | <a href="https://codepen.io/kent_iyo/pen/GRJJjKZ" class="uk-text-muted" title="A pen by Kent" target="_blank" data-uk-tooltip>Kent</a>
				</div>
			</div>
		</div>
	</div>
</footer>
              
            
!

CSS

              
                .file-upload-name {
	background: #585858;
	padding: 0.5rem;
	color: white;
	margin-top: 0.2rem;
}

.alert {
	border-left: 1px solid;
}

#img-preview-responsive {
	min-height: 200px;
}

              
            
!

JS

              
                const UPLOAD = {
	BASE_PATH: window.location.origin,
	ACCEPTED_DOC_MIMES: [
		"image/png",
		"image/jpeg",
		"application/pdf",
		"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
		"application/msword"
	],
	DOC_MIMES: [
		"application/pdf",
		"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
		"application/msword"
	],
	FileInputs: [...document.querySelectorAll("input[type='file']")],
	Init: () => {
		UPLOAD.AddEventListeners();
	},
	AddEventListeners: () => {
		UPLOAD.FileInputs.map((fileInput) => {
			fileInput.addEventListener("change", UPLOAD.HandleFileChange);
		});

		UPLOAD.FileInputs.map((fileInput) => {
			fileInput.addEventListener("blur", (e) => {
				// console.log(e.target);
			});
		});

		UPLOAD.FileInputs.map((fileInput) => {
			const box = fileInput.closest(".uk-placeholder");
			box.addEventListener(
				"dragover",
				(e) => {
					e.preventDefault();
					e.stopPropagation();
					box.classList.add("uk-box-shadow-medium");
				},
				false
			);
		});

		UPLOAD.FileInputs.map((fileInput) => {
			const box = fileInput.closest(".uk-placeholder");
			box.addEventListener(
				"dragleave",
				(e) => {
					e.preventDefault();
					e.stopPropagation();
					box.classList.remove("uk-box-shadow-medium");
				},
				false
			);
		});

		UPLOAD.FileInputs.map((fileInput) => {
			const box = fileInput.closest(".uk-placeholder");
			box.addEventListener(
				"drop",
				(e) => {
					e.preventDefault();
					e.stopPropagation();
					box.classList.remove("uk-box-shadow-medium");

					const dropZone = e.target.closest(".drop__zone");
					const componentContainer = dropZone.closest(".uk-upload-box");
					let preview, alertBox, alertMsg;

					preview = dropZone.querySelector(`#preview`);
					alertBox = componentContainer.querySelector(`#error-alert`);
					alertMsg = componentContainer.querySelector(`#error-messages`);

					let files;
					let isMultiple = false;

					if (fileInput.hasAttribute("multiple")) {
						isMultiple = true;
					}

					if (!e.dataTransfer.files.length) return;

					/** If items dropped are more than one file and the input doesn't accept multiple, do not accept the files. Clear preview field, show notification and add shake animatin to dropzone */
					if (isMultiple === false && e.dataTransfer.files.length > 1) {
						UPLOAD.RemoveChild(preview);
						UPLOAD.AddShakeAnimation(dropZone);

						UIkit.notification({
							message: "Cannot accept multiple files when expecting a single file!",
							status: "danger",
							pos: "center",
							timeout: 4000
						});
						return;
					}

					files = e.dataTransfer.files;
					fileInput.files = files;

					const options = {
						isMultiple,
						files,
						preview,
						alertBox,
						alertMsg,
						fileInput,
						dropZone
					};

					if (isMultiple) {
						UPLOAD.PreviewMultipleDocs(options);
					} else {
						UPLOAD.PreviewSingleDoc(options);
					}
				},
				false
			);
		});
	},
	Reset: (e) => {
		if (e.target.files.length == 0) {
			const alertWrapper = e.target.closest("#error-alert");
			let alertMessage = alertWrapper.querySelector("#error-messages");
			alertWrapper.classList.add("uk-hidden");
			alertMessage.textContent = "";
			input.value = "";
			return;
		}
	},
	AddShakeAnimation: (parent) => {
		let timeout;
		clearTimeout(timeout);
		parent.classList.add("uk-animation", "uk-animation-shake");
		timeout = setTimeout(() => {
			parent.classList.remove("uk-animation", "uk-animation-shake");
		}, 1000);
	},
	HandleFileChange: (e) => {
		let files;
		let isMultiple = false;

		if (e.target.hasAttribute("multiple")) {
			isMultiple = true;
		}

		const dropZone = e.target.closest(".drop__zone");
		const componentContainer = dropZone.closest(".uk-upload-box");
		const documentCategory = dropZone.dataset.documentCategory;
		let preview, alertBox, alertMsg;
		preview = dropZone.querySelector(`#preview`);
		alertBox = componentContainer.querySelector(`#error-alert`);
		alertMsg = componentContainer.querySelector(`#error-messages`);

		/** Remove existing files and preview files if files lenght is 0 */
		if (!e.target.files.length) {
			UPLOAD.RemoveChild(preview);
			return;
		}

		files = e.target.files;
		const options = {
			files,
			fileInput: e.target,
			isMultiple,
			preview,
			alertBox,
			alertMsg,
			dropZone
		};

		if (isMultiple) {
			UPLOAD.PreviewMultipleDocs(options);
		} else {
			UPLOAD.PreviewSingleDoc(options);
		}
	},
	RemoveChild: (preview) => {
		while (preview.firstChild) {
			preview.removeChild(preview.firstChild);
		}
	},
	ImgPreviewLi: (readerResult, filename) => {
		const li = document.createElement("li");
		const div = document.createElement("div");
		const img = document.createElement("img");
		const span = document.createElement("span");

		li.className = "list-item uk-margin-medium-top";
		div.className = "uk-cover-container";
		img.setAttribute("id", "img-preview-responsive");
		img.setAttribute("src", readerResult);
		img.setAttribute("data-name", filename);
		img.setAttribute("alt", "file-image-preview");
		span.className = "uk-text-meta uk-text-break file-upload-name";
		span.textContent = filename;

		div.append(img);
		li.append(div, span);
		return li;
	},
	ValidateFileType: ({
		fileType,
		fileInput,
		alertBox,
		alertMsg,
		preview,
		dropZone
	}) => {
		if (!UPLOAD.ACCEPTED_DOC_MIMES.includes(fileType)) {
			alertMsg.textContent =
				"Sorry, one or more of your file type is not allowed.";
			alertMsg.classList.add("uk-text-danger");
			alertBox.classList.remove("uk-hidden");
			fileInput.value = "";
			UPLOAD.AddShakeAnimation(dropZone);
			UPLOAD.RemoveChild(preview);
			return false;
		}
		return true;
	},
	ValidateFileSize: ({
		fileInput,
		size,
		alertBox,
		alertMsg,
		preview,
		dropZone
	}) => {
		if (size > 2000000) {
			alertMsg.textContent =
				"Sorry, one or more of your files has exceeded the file size limit of 2MB.";
			alertMsg.classList.add("uk-text-danger");
			alertBox.classList.remove("uk-hidden");
			fileInput.value = "";
			UPLOAD.AddShakeAnimation(dropZone);
			UPLOAD.RemoveChild(preview);
			return false;
		}
		return true;
	},
	PreviewMultipleDocs: ({
		files,
		fileInput,
		preview,
		alertBox,
		alertMsg,
		dropZone
	}) => {
		const docFiles = [...files];

		docFiles.forEach((file) => {
			const size = file["size"];
			const fileType = file["type"];

			if (docFiles.length !== 0) {
				UPLOAD.RemoveChild(preview);
			}

			const fileOptions = {
				fileInput,
				preview,
				alertBox,
				alertMsg,
				fileType,
				size,
				dropZone
			};

			if (!UPLOAD.ValidateFileSize(fileOptions)) return;

			if (!UPLOAD.ValidateFileType(fileOptions)) return;

			alertMsg.textContent = "";
			alertBox.classList.add("uk-hidden");
			fileInput.files = files;
			const reader = new FileReader();
			reader.readAsDataURL(file);
			reader.onload = () => {
				let filename = file["name"];
				let imgPreview = "";
				if (UPLOAD.DOC_MIMES.includes(fileType)) {
					if (fileType === "application/pdf") {
						imagePath = `${UPLOAD.BASE_PATH}/assets/images/doc/pdf.svg`;
						imgPreview = UPLOAD.ImgPreviewLi(imagePath, filename);
					} else {
						imagePath = `${UPLOAD.BASE_PATH}/assets/images/doc/docx.svg`;
						imgPreview = UPLOAD.ImgPreviewLi(imagePath, filename);
					}
				} else {
					imgPreview = UPLOAD.ImgPreviewLi(reader.result, filename);
				}
				preview.append(imgPreview);
			};
		});
	},
	PreviewSingleDoc: ({
		files,
		fileInput,
		preview,
		alertBox,
		alertMsg,
		dropZone
	}) => {
		const size = files[0]["size"];
		const fileType = files[0]["type"];
		let filename = files[0]["name"];

		if (files[0].length !== 0) {
			UPLOAD.RemoveChild(preview);
		}

		const fileOptions = {
			fileInput,
			preview,
			alertBox,
			alertMsg,
			fileType,
			size,
			dropZone
		};

		if (!UPLOAD.ValidateFileSize(fileOptions)) return;

		if (!UPLOAD.ValidateFileType(fileOptions)) return;

		alertMsg.textContent = "";
		alertBox.classList.add("uk-hidden");

		const reader = new FileReader();
		reader.readAsDataURL(files[0]);
		reader.onload = () => {
			let imgPreview = "";
			let imagePath = "";
			if (UPLOAD.DOC_MIMES.includes(fileType)) {
				if (fileType === "application/pdf") {
					imagePath = `${UPLOAD.BASE_PATH}/assets/images/doc/pdf.svg`;
					imgPreview = UPLOAD.ImgPreviewLi(imagePath, filename);
				} else {
					imagePath = `${UPLOAD.BASE_PATH}/assets/images/doc/docx.svg`;
					imgPreview = UPLOAD.ImgPreviewLi(imagePath, filename);
				}
			} else {
				imgPreview = UPLOAD.ImgPreviewLi(reader.result, filename);
			}
			preview.append(imgPreview);
		};
	}
};

document.addEventListener("DOMContentLoaded", UPLOAD.Init());

              
            
!
999px

Console