ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Preview PDF before upload using FileUpload control and jQuery in ASP.Net<p>Hi <a class="username" rel="SajidHussa">SajidHussa</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;html xmlns=&#34;http://www.w3.org/1999/xhtml&#34;&gt; &lt;head runat=&#34;server&#34;&gt; &lt;title&gt;&lt;/title&gt; &lt;script type=&#34;text/javascript&#34; src=&#39;https://ajax.aspnetcdn.com/ajax/jQuery/jquery-1.8.3.min.js&#39;&gt;&lt;/script&gt; &lt;script type=&#34;text/javascript&#34; src=&#39;https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/js/bootstrap.min.js&#39;&gt;&lt;/script&gt; &lt;link rel=&#34;stylesheet&#34; href=&#39;https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.0.3/css/bootstrap.min.css&#39; media=&#34;screen&#34; /&gt; &lt;script type=&#34;text/javascript&#34;&gt; function ShowPopup(title, body) { $(&#34;#MyPopup .modal-title&#34;).html(title); $(&#34;#MyPopup .modal-body&#34;).html(body); $(&#34;#MyPopup&#34;).modal(&#34;show&#34;); } $(function () { $(&#34;#fuOpen&#34;).change(function () { $(&#34;#dvPreview&#34;).html(&#34;&#34;); var regex = /^([a-zA-Z0-9\s_\\.\-:])+(.pdf)$/; if (regex.test($(this).val().toLowerCase())) { if ($.browser.msie &amp;&amp; parseFloat(jQuery.browser.version) &lt;= 9.0) { $(&#34;#dvPreview&#34;).show(); $(&#34;#dvPreview&#34;)[0].filters.item(&#34;DXImageTransform.Microsoft.AlphaImageLoader&#34;).src = $(this).val(); } else { if (typeof (FileReader) != &#34;undefined&#34;) { $(&#34;#dvPreview&#34;).show(); $(&#34;#dvPreview&#34;).append(&#34;&lt;iframe /&gt;&#34;); var reader = new FileReader(); reader.onload = function (e) { $(&#34;#dvPreview iframe&#34;).attr(&#34;src&#34;, e.target.result); } reader.readAsDataURL($(this)[0].files[0]); } else { alert(&#34;This browser does not support FileReader.&#34;); } } } else { alert(&#34;Please upload a valid pdf file.&#34;); } }); }); &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;form id=&#34;form1&#34; runat=&#34;server&#34;&gt; &lt;asp:FileUpload runat=&#34;server&#34; ID=&#34;fuOpen&#34; /&gt; &lt;br /&gt; &lt;b&gt;Live Preview&lt;/b&gt; &lt;br /&gt; &lt;br /&gt; &lt;div id=&#34;dvPreview&#34;&gt; &lt;/div&gt; &lt;br /&gt; &lt;asp:Button Text=&#34;Upload&#34; runat=&#34;server&#34; ID=&#34;btnShow&#34; OnClick=&#34;Upload&#34; /&gt; &lt;!-- Bootstrap --&gt; &lt;!-- Modal Popup --&gt; &lt;div id=&#34;MyPopup&#34; class=&#34;modal fade&#34; role=&#34;dialog&#34;&gt; &lt;div class=&#34;modal-dialog&#34;&gt; &lt;!-- Modal content--&gt; &lt;div class=&#34;modal-content&#34;&gt; &lt;div class=&#34;modal-header&#34;&gt; &lt;button type=&#34;button&#34; class=&#34;close&#34; data-dismiss=&#34;modal&#34;&gt; &amp;times;&lt;/button&gt; &lt;h4 class=&#34;modal-title&#34;&gt; &lt;/h4&gt; &lt;/div&gt; &lt;div class=&#34;modal-body&#34;&gt; &lt;/div&gt; &lt;div class=&#34;modal-footer&#34;&gt; &lt;button type=&#34;button&#34; class=&#34;btn btn-danger&#34; data-dismiss=&#34;modal&#34;&gt; Close&lt;/button&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;/div&gt; &lt;div&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt;</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">protected void Upload(object sender, EventArgs e) { fuOpen.SaveAs(Server.MapPath(&#34;~/Files/&#34; + fuOpen.FileName)); string fileName = fuOpen.FileName; string embed = &#34;&lt;object data=\&#34;{0}\&#34; type=\&#34;application/pdf\&#34; width=\&#34;500px\&#34; height=\&#34;300px\&#34;&gt;&#34;; embed += &#34;If you are unable to view file, you can download from &lt;a href = \&#34;{0}\&#34;&gt;here&lt;/a&gt;&#34;; embed += &#34; or download &lt;a target = \&#34;_blank\&#34; href = \&#34;http://get.adobe.com/reader/\&#34;&gt;Adobe PDF Reader&lt;/a&gt; to view the file.&#34;; embed += &#34;&lt;/object&gt;&#34;; string title = fileName; ClientScript.RegisterStartupScript(this.GetType(), &#34;Popup&#34;, &#34;ShowPopup(&#39;&#34; + title + &#34;&#39;, &#39;&#34; + string.Format(embed, ResolveUrl(string.Format(&#34;~/Files/{0}&#34;, fileName))) + &#34;&#39;);&#34;, true); }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Upload(ByVal sender As Object, ByVal e As EventArgs) fuOpen.SaveAs(Server.MapPath(&#34;~/Files/&#34; &amp; fuOpen.FileName)) Dim fileName As String = fuOpen.FileName Dim filePath As String = Server.MapPath(&#34;~/Files/&#34; &amp; fuOpen.FileName) Dim embed As String = &#34;&lt;object data=&#34;&#34;{0}&#34;&#34; type=&#34;&#34;application/pdf&#34;&#34; width=&#34;&#34;500px&#34;&#34; height=&#34;&#34;300px&#34;&#34;&gt;&#34; embed += &#34;If you are unable to view file, you can download from &lt;a href = &#34;&#34;{0}&#34;&#34;&gt;here&lt;/a&gt;&#34; embed += &#34; or download &lt;a target = &#34;&#34;_blank&#34;&#34; href = &#34;&#34;http://get.adobe.com/reader/&#34;&#34;&gt;Adobe PDF Reader&lt;/a&gt; to view the file.&#34; embed += &#34;&lt;/object&gt;&#34; Dim title As String = fileName ClientScript.RegisterStartupScript(Me.GetType(), &#34;Popup&#34;, &#34;ShowPopup(&#39;&#34; &amp; title &amp; &#34;&#39;, &#39;&#34; &amp; String.Format(embed, ResolveUrl(String.Format(&#34;~/Files/{0}&#34;, fileName))) &amp; &#34;&#39;);&#34;, True) End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/S1M4aXi.gif" alt="" width="387" height="409" /></p>https://www.aspforums.net:443/Threads/136362/Preview-PDF-before-upload-using-FileUpload-control-and-jQuery-in-ASPNet/https://www.aspforums.net:443/Threads/136362/Preview-PDF-before-upload-using-FileUpload-control-and-jQuery-in-ASPNet/Thu, 18 Jul 2019 02:08:02 GMT