title: ASP.NET C# Uploading the file using modal popup by Twitter Bootstrap 3.0
body: <p>Hello I am making use of bootstrap's popup modal for user to upload the image. This is how it looks like..</p>
  
  <p><img src="https://i.stack.imgur.com/oK5rT.png" alt="enter image description here"></p>
  
  <p>How does it works is user have to click the button for the modal popup to appear. After that, user will have to click upload image and the picture preview will appear.</p>
  
  <p>Like this..
  <img src="https://i.stack.imgur.com/dY4nC.png" alt="enter image description here"></p>
  
  <p>The problem is that once the user have chosen the file after clicking file upload button, the page refreshes itself. Thus, forcing the user to click the button again for the modal to popup. Fortunately, the preview picture appears after the user have clicked the button.</p>
  
  <p>Here is the source code
  test.aspx:</p>
  
  <pre><code>&lt;%@ Page Title="" Language="C#" MasterPageFile="~/MasterPage.Master" AutoEventWireup="true" CodeBehind="test.aspx.cs" Inherits="fileuploadpreview.test" %&gt;
  &lt;asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server"&gt;
  &lt;/asp:Content&gt;
  
  &lt;asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server"&gt;
  
      &lt;!-- jQuery (necessary for Bootstrap's JavaScript plugins) --&gt;
          &lt;script src="//code.jquery.com/jquery.js"&gt;&lt;/script&gt;
  
  
  
  &lt;script language="javascript" type="text/javascript"&gt;
  
      function UploadFileNow() {
  
          var value = $("#FileUpload2").val();
  
          if (value != '') {
  
              $("#form1").submit();
  
          }
  
      };
  &lt;/script&gt;
  
      &lt;div&gt;
  
          &lt;h2&gt;Upload file without Submit button&lt;/h2&gt;
  
          &lt;asp:Button ID="btn_test" runat="server" Text="Test" data-target="#myModal" data-toggle="modal" CssClass="btn btn-primary" /&gt;
  
  
  
          &lt;div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true"&gt;
          &lt;div class="modal-dialog"&gt;
          &lt;div class="modal-content"&gt;
              &lt;div class="modal-header"&gt;
              &lt;button type="button" class="close" data-dismiss="modal" aria-hidden="true"&gt;&amp;times;&lt;/button&gt;
              &lt;h4 class="modal-title" id="myModalLabel"&gt;Modal title&lt;/h4&gt;
              &lt;/div&gt;
              &lt;div class="modal-body"&gt;
              &lt;asp:Label ID="lblMessage" runat="server" ForeColor="Green" /&gt;
  
                  Select file and upload will start automatically
  
                  &lt;h5&gt;Change Profile Picture&lt;/h5&gt;             
  
  
                  &lt;asp:FileUpload ID="FileUpload2" runat="server" ClientIDMode="Static" onchange="UploadFileNow()"/&gt;
  
                  &lt;br /&gt;
                  &lt;br /&gt;
  
                  &lt;asp:Label ID="lbl_profilepicturePreview" runat="server" Text="The preview of your updated profile picture: "&gt;&lt;/asp:Label&gt;
                  &lt;asp:Image ID="img" runat="server" /&gt;
                  &lt;br /&gt;
                  &lt;asp:Label ID="lbl_profilepicture" runat="server"&gt;&lt;/asp:Label&gt;
                  &lt;br /&gt;
  
              &lt;/div&gt;
              &lt;div class="modal-footer"&gt;
                  &lt;asp:Button ID="btn_profilepicturesubmit" runat="server" CssClass="btn btn-primary" Text="Save Profile Picture" /&gt;                   
              &lt;/div&gt;
          &lt;/div&gt;&lt;!-- /.modal-content --&gt;
          &lt;/div&gt;&lt;!-- /.modal-dialog --&gt;
      &lt;/div&gt;&lt;!-- /.modal --&gt;
  
      &lt;/div&gt;
  
  &lt;/asp:Content&gt;
  </code></pre>
  
  <p>Next test.aspx.cs:</p>
  
  <pre><code>using System.IO;
  using SD = System.Drawing;
  using System.Drawing.Imaging;
  using System.Drawing.Drawing2D;
  using System;
  using System.Web;
  
  namespace fileuploadpreview
  {
      public partial class test : System.Web.UI.Page
      {
          protected void Page_Load(object sender, EventArgs e)
          {
              if (IsPostBack &amp;&amp; FileUpload2.PostedFile != null)
              {
  
                  if (FileUpload2.PostedFile.FileName.Length &gt; 0)
                  {
  
                      Session["Image"] = FileUpload2.PostedFile;
                      Stream fs = FileUpload2.PostedFile.InputStream;
                      BinaryReader br = new BinaryReader(fs);
                      byte[] bytes = br.ReadBytes((Int32)fs.Length);
                      string base64String = Convert.ToBase64String(bytes, 0, bytes.Length);
                      img.ImageUrl = "data:image/png;base64," + base64String;
  
                  }
  
              }
          }
      }
  } 
  </code></pre>