Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Problem is IE wouldn't let you submit a file through javascript, user have to click the file input. It is a known problem, described here:</p> <p><code>When an file-input is opened via a scripted, forced click() event, IE won't let you submit the form. If you click the file-input via your own mouse (what we don't want), IE will let you submit the form.</code><a href="https://stackoverflow.com/a/13954595/690329">Open file input dialog and upload onchange possible in IE?</a></p> <p>and also here: <a href="https://stackoverflow.com/q/14560121/690329">File Upload Using Javascript returns 'Access Denied' Error With Stylized to a button</a></p> <p>So, you have to trick the user, make the file-input transparent and place your button under the file-input. When user will click your button, they will click the file input instead.</p> <p>With the css (probably you will need to tweak it) your markup should look like below:</p> <pre><code>&lt;%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default.aspx.vb" Inherits="_Default" %&gt; &lt;!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"&gt; &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; &lt;head id="Head1" runat="server"&gt; &lt;style type="text/css"&gt; .fileContainer { overflow: hidden; position: relative; } #FileUploaddd2 { position: relative; text-align: right; -moz-opacity: 0; filter: alpha(opacity: 0); opacity: 0; z-index: 2; left: -130px; } #Button2 { position: absolute; top: 0px; left: 0px; z-index: 1; } &lt;/style&gt; &lt;title&gt;&lt;/title&gt; &lt;/head&gt; &lt;body&gt; &lt;form id="form1" runat="server"&gt; &lt;div&gt; &lt;label class="fileContainer"&gt; &lt;input id="Button2" type="button" value="Attach a File" /&gt; &lt;input type="file" id="FileUploaddd2" runat="Server" /&gt; &lt;/label&gt; &lt;br /&gt; &lt;br /&gt; &lt;asp:Button ID="btnSubmit" runat="server" BorderColor="Black" BorderStyle="Solid" BorderWidth="1px" Text="Submit" /&gt; &lt;/div&gt; &lt;/form&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>And in the code behind you can catch the submitted file</p> <pre><code>Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click Dim file As System.Web.HttpPostedFile = FileUploaddd2.PostedFile If Not file Is Nothing Then 'Save file? End If End Sub </code></pre> <p><strong>EDIT:</strong> If you want to show the filename in a label, you can do the following:</p> <p>In Input file's change event call a jsfunction: </p> <pre><code>&lt;input type="file" id="FileUploaddd2" runat="Server" onchange="setlabelvalue();" /&gt; </code></pre> <p>Add a label to display filename:</p> <pre><code>&lt;asp:Label ID="lblFileName" runat ="server" Text=""&gt;&lt;/asp:Label&gt; </code></pre> <p>Add the setlabelvalue() js function: </p> <pre><code>function setlabelvalue() { var filename = document.getElementById("FileUploaddd2").value; var lastIndex = filename.lastIndexOf("\\"); if (lastIndex &gt;= 0) { filename = filename.substring(lastIndex + 1); } document.getElementById('&lt;%=lblFileName.ClientID%&gt;').innerHTML = filename; } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload