Skip to content

Commit 1c5be5d

Browse files
github-actions[bot]KB Bot
andauthored
Added new kb article asyncupload-display-pdf-preview-file-selection (#721)
Co-authored-by: KB Bot <[email protected]>
1 parent eb6fff1 commit 1c5be5d

File tree

1 file changed

+96
-0
lines changed

1 file changed

+96
-0
lines changed
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
---
2+
title: Displaying PDF Preview on File Selection with RadAsyncUpload
3+
description: Learn how to display a PDF preview in a SlidingPanel when using RadAsyncUpload in UI for ASP.NET AJAX, immediately after file selection.
4+
type: how-to
5+
page_title: How to Show PDF Preview After File Selection in UI for ASP.NET AJAX
6+
meta_title: Show PDF Preview After File Selection in RadAsyncUpload
7+
slug: asyncupload-display-pdf-preview-file-selection
8+
tags: asyncupload, slidingpanel, pdf, preview, file-selected, ui-for-aspnet-ajax
9+
res_type: kb
10+
ticketid: 1701830
11+
---
12+
13+
## Environment
14+
<table>
15+
<tbody>
16+
<tr>
17+
<td>Product</td>
18+
<td>AsyncUpload for ASP.NET AJAX</td>
19+
</tr>
20+
<tr>
21+
<td>Version</td>
22+
<td>All</td>
23+
</tr>
24+
</tbody>
25+
</table>
26+
27+
## Description
28+
29+
I need to display a PDF preview immediately after selecting a file in [RadAsyncUpload](https://www.telerik.com/products/aspnet-ajax/documentation/controls/asyncupload/overview). The PDF content must be shown before the file upload event is triggered, and other instructions are executed during the upload process.
30+
31+
This knowledge base article also answers the following questions:
32+
33+
- How to show a PDF preview when selecting a file in RadAsyncUpload?
34+
- How to use FileReader API with RadAsyncUpload for PDF preview?
35+
36+
## Solution
37+
38+
To display a PDF preview immediately after file selection in RadAsyncUpload, use the `FileReader` API in combination with the `OnClientFileSelected` event. RadAsyncUpload does not provide built-in PDF preview functionality before upload, but this method allows you to achieve the desired behavior.
39+
40+
1. Configure RadAsyncUpload with the `OnClientFileSelected` event.
41+
2. Override the private `_onFileSelected` function to access the file object in the client-side event.
42+
3. Use the `FileReader` API to read the selected PDF file.
43+
4. Display the PDF content in an `<iframe>` for preview.
44+
45+
````ASP.NET
46+
<telerik:RadAsyncUpload runat="server" ID="RadAsyncUpload1" OnClientFileSelected="previewPdfOnSelect" />
47+
<telerik:RadSlidingPane ID="PaneAnteprima" runat="server" Title="Anteprima File" DockOnOpen="True">
48+
<iframe id="pdfPreview" style="width: 100%; height: 600px;"></iframe>
49+
</telerik:RadSlidingPane>
50+
````
51+
52+
````JavaScript
53+
// Override to make the file accessible in OnClientFileSelected event
54+
Telerik.Web.UI.RadAsyncUpload.prototype._onFileSelected = function (row, fileInput, fileName, shouldAddNewInput, file) {
55+
let args = {
56+
row: row,
57+
fileInputField: fileInput,
58+
file: file
59+
};
60+
61+
args.rowIndex = $(row).index();
62+
args.fileName = fileName;
63+
this._selectedFilesCount++;
64+
65+
shouldAddNewInput = shouldAddNewInput && (this.get_maxFileCount() === 0 || this._selectedFilesCount < this.get_maxFileCount());
66+
this._marshalUpload(row, fileName, shouldAddNewInput);
67+
let labels = $("label", row);
68+
69+
if (labels.length > 0) labels.remove();
70+
71+
$.raiseControlEvent(this, "fileSelected", args);
72+
};
73+
74+
function previewPdfOnSelect(sender, args) {
75+
let file = args.get_file();
76+
77+
if (file?.type === "application/pdf") {
78+
let reader = new FileReader();
79+
80+
reader.onload = function (e) {
81+
document.getElementById("pdfPreview").src = e.target.result;
82+
};
83+
84+
reader.readAsDataURL(file);
85+
}
86+
}
87+
````
88+
89+
This solution ensures that the PDF content is displayed immediately after file selection without triggering the upload event.
90+
91+
## See Also
92+
93+
- [RadAsyncUpload Overview](https://www.telerik.com/products/aspnet-ajax/documentation/controls/asyncupload/overview)
94+
- [Preview Uploaded Image with RadAsyncUpload](https://www.telerik.com/products/aspnet-ajax/documentation/knowledge-base/asyncupload-preview-uploaded-image#preview-uploaded-image-with-radasyncupload)
95+
- [Access Selected File in OnClientFileSelected Event of AsyncUpload](https://www.telerik.com/products/aspnet-ajax/documentation/knowledge-base/asyncupload-access-selected-file-in-the-arguments-of-onclientfileselected-event#access-selected-file-in-the-arguments-of-onclientfileselected-event-of-asyncupload)
96+
---

0 commit comments

Comments
 (0)