@@ -26,3 +26,223 @@ Previously selected files.
2626Newly selected files.
2727
2828---
29+
30+ The following code snippet demonstrates how to add image previews before uploading. It uses [ File API] ( https://developer.mozilla.org/en-US/docs/Web/API/File_API ) to get image details and create a thumbnail.
31+
32+ ---
33+ ##### jQuery
34+
35+ <!-- tab: index.html -->
36+ <div id="fileUploader"></div>
37+ <div id="list">
38+
39+ <!-- tab: index.js -->
40+ $(function () {
41+ function previewImages(file) {
42+ const reader = new FileReader();
43+ reader.onload = (function (theFile) {
44+ return function (e) {
45+ const span = $("<span>");
46+ span.html(
47+ [
48+ '<img class="thumb" src="',
49+ e.target.result,
50+ '" title="',
51+ escape(theFile.name),
52+ '"/>'
53+ ].join("")
54+ );
55+ $("#list").append(span);
56+ };
57+ })(file);
58+ reader.readAsDataURL(file);
59+ }
60+
61+ const fileUploader = $("#fileUploader").dxFileUploader({
62+ accept: "image/*",
63+ uploadMode: "useButtons",
64+ multiple: true,
65+ onValueChanged: function (args) {
66+ const files = args.value;
67+ $("#list").html("");
68+ if (files.length == 0) return;
69+ for (let i = 0; i < files.length; i+=1) {
70+ const file = files[i];
71+ previewImages(file);
72+ }
73+ }
74+ }).dxFileUploader("instance");
75+ });
76+
77+ <!-- tab: index.css -->
78+ .thumb {
79+ height: 150px;
80+ max-width: 150px;
81+ border: 1px solid #000;
82+ margin: 10px 5px 0 0;
83+ }
84+
85+ ##### Angular
86+
87+ <!-- tab: app.component.html -->
88+ <dx-file-uploader
89+ [multiple]="true"
90+ uploadMode="useButtons"
91+ accept="image/*"
92+ (onValueChanged)="onFileSelected($event)"
93+ >
94+ </dx-file-uploader>
95+ <div id="list">
96+ <span *ngFor="let image of imagePreviews">
97+ <img class="thumb" [src]="image.src" [title]="image.name" />
98+ </span>
99+ </div>
100+
101+ <!-- tab: app.component.ts -->
102+ import { DxFileUploaderTypes } from 'devextreme-angular/file-uploader';
103+ // ...
104+ export class AppComponent {
105+ imagePreviews: { src: string; name: string }[] = [];
106+
107+ onFileSelected(e: DxFileUploaderTypes.ValueChangedEvent) {
108+ const input = e.value;
109+ this.imagePreviews = [];
110+ input.forEach((file) => {
111+ const reader = new FileReader();
112+ reader.onload = () => {
113+ this.imagePreviews.push({
114+ src: reader.result as string,
115+ name: file.name,
116+ });
117+ };
118+ reader.readAsDataURL(file);
119+ });
120+ }
121+ }
122+
123+ <!-- tab: app.component.css -->
124+ .thumb {
125+ height: 150px;
126+ max-width: 150px;
127+ border: 1px solid #000;
128+ margin: 10px 5px 0 0;
129+ }
130+
131+ ##### Vue
132+
133+ <!-- tab: App.vue -->
134+ <template>
135+ <DxFileUploader
136+ :multiple="true"
137+ accept="image/*"
138+ upload-mode="useButtons"
139+ @value-changed="onFileSelected"
140+ />
141+ <div id="list">
142+ <span v-for="(image, index) in imagePreviews" :key="index">
143+ <img class="thumb" :src="image.src" :title="image.name" />
144+ </span>
145+ </div>
146+ </template>
147+ <script setup lang="ts">
148+ import { ref } from "vue";
149+ import DxFileUploader from "devextreme-vue/file-uploader";
150+ import type { DxFileUploaderTypes } from "devextreme-vue/file-uploader";
151+
152+ interface ImagePreview {
153+ src: string;
154+ name: string;
155+ }
156+
157+ const imagePreviews = ref<ImagePreview[]>([]);
158+
159+ const onFileSelected = (e: DxFileUploaderTypes.ValueChangedEvent) => {
160+ const input = e.value;
161+ imagePreviews.value = [];
162+ input.forEach((file) => {
163+ const reader = new FileReader();
164+ reader.onload = () => {
165+ imagePreviews.value.push({
166+ src: reader.result as string,
167+ name: file.name,
168+ });
169+ };
170+ reader.readAsDataURL(file);
171+ });
172+ };
173+ </script>
174+ <style>
175+ .thumb {
176+ height: 150px;
177+ max-width: 150px;
178+ border: 1px solid #000;
179+ margin: 10px 5px 0 0;
180+ }
181+ </style>
182+
183+ ##### React
184+
185+ <!-- tab: App.tsx -->
186+ import React, { useCallback, useState } from "react";
187+ import FileUploader, { FileUploaderTypes } from "devextreme-react/file-uploader";
188+
189+ interface ImagePreview {
190+ src: string;
191+ name: string;
192+ }
193+
194+ export default function App() {
195+ const [imagePreviews, setImagePreviews] = useState<ImagePreview[]>([]);
196+ const onFileSelected = useCallback((e: FileUploaderTypes.ValueChangedEvent) => {
197+ const input = e.value;
198+ setImagePreviews([]);
199+ input.forEach((file) => {
200+ const reader = new FileReader();
201+ reader.onload = () => {
202+ setImagePreviews((prevImages) => [
203+ ...prevImages,
204+ { src: reader.result, name: file.name },
205+ ]);
206+ };
207+ reader.readAsDataURL(file);
208+ });
209+ }, []);
210+ return (
211+ <div>
212+ <FileUploader
213+ multiple={true}
214+ uploadMode="useButtons"
215+ accept="image/*"
216+ onValueChanged={onFileSelected}
217+ />
218+ <div id="list">
219+ {imagePreviews.map((image, index) => (
220+ <span key={index}>
221+ <img
222+ className="thumb"
223+ src={image.src}
224+ title={image.name}
225+ alt={image.name}
226+ />
227+ </span>
228+ ))}
229+ </div>
230+ </div>
231+ );
232+ }
233+
234+ <!-- tab: styles.css -->
235+ .thumb {
236+ height: 150px;
237+ max-width: 150px;
238+ border: 1px solid #000;
239+ margin: 10px 5px 0 0;
240+ }
241+
242+ ---
243+
244+ For details on how to retrieve file data after uploading, refer to the following demo:
245+
246+ #include btn-open-demo with {
247+ href: "https://js.devexpress.com/Demos/WidgetsGallery/Demo/FileUploader/CustomDropzone/ "
248+ }
0 commit comments