Alexander Dracka

Alexander Dracka

  • NA
  • 16
  • 13.4k

How fill TexBox by filepath from FileUpload control

Aug 17 2013 10:37 AM
Hello i got hidden FileUpload control, textBox where i wanna write url adrres or! filepath to some file, and button for call FileUpload. My problem is that I need  instantly after user checks in fileupload control some file and close popup window, fill TextBox with filepaths of that file. I know how show filepath but i need do it automatically and that trap for me. Here is what i got so far. Last piece of code is how i got filepath but I need that user use FileUpload 
protected void Page_Load(object sender, EventArgs e)
{
  btnFileUpload.Attributes.Add(
    "onclick",
    "document.getElementById('" + FileUpload2.ClientID + "').click();");  
}
<div class="ViewContent">
  <asp:FileUpload ID="FileUpload2" runat="server" Width="317px" style="display: none"/>
  <input id="btnFileUpload" type="button" value="Add" runat="server" style="width: 70px" />
    <asp:TextBox ID="TextBox2" runat="server" Width="310px"/>
</div>
if (FileUpload2.PostedFile != null)
{
  TextBox2.Text = System.IO.Path.GetFullPath(FileUpload2.PostedFile.FileName);
}

Answers (10)

1
Alexander Dracka

Alexander Dracka

  • 0
  • 16
  • 13.4k
Aug 18 2013 12:27 PM
OK so i found an answer. I cant get full filepath for security issues. But here is a realy smart jquery function to show after Click name of file by TextBox
0
Iftikar Hussain

Iftikar Hussain

  • 0
  • 12.6k
  • 449.2k
Aug 17 2013 12:21 PM
ok. Let me know if you need anything else

Regards,
Iftikar
0
Alexander Dracka

Alexander Dracka

  • 0
  • 16
  • 13.4k
Aug 17 2013 12:20 PM
i dont think so that problem can be in display:none. I tried it but without progress. But i agree that this method is good Thank you i Will try it in clean page and we will see. Cause whenn i tried simpler alert function in onchange event aler was fire.
0
Iftikar Hussain

Iftikar Hussain

  • 0
  • 12.6k
  • 449.2k
Aug 17 2013 12:11 PM
since you set display:none, therefore onchange event was not fired. You need to set opacity value and onchange event will fire

<asp:FileUpload ID="FileUpload2" runat="server" Width="317px"  cssclass="fileUpload" onchange="javascript:SetFilePath();"/>

function SetFilePath()
{
  var filePath = document.getElementById("FileUpload2").value;
document.getElementById("TextBox2").value=filePath;
}

Regards,
Iftikar

0
Alexander Dracka

Alexander Dracka

  • 0
  • 16
  • 13.4k
Aug 17 2013 11:58 AM
maybe you dont understand my problem. I need save filepath from FileUpload control to my TextBox. I need something like this code but dont work:
function Test()  {
 var fu1 = document.getElementById("FileUpload2");  document.getElementById("TextBox2").value = fu1.value;  }
0
Iftikar Hussain

Iftikar Hussain

  • 0
  • 12.6k
  • 449.2k
Aug 17 2013 11:55 AM
try like this
in .css


.fileUpload  
{
    position: absolute;
    top: 0px; 
    left: 0px; 
    opacity: -10; 
    filter: alpha(opacity=-10);
    width: 100px; 
    height: 20px;
}

<asp:FileUpload ID="FileUpload2" runat="server" Width="317px" cssclass="fileUpload"/>

Regards,
Iftikar
0
Alexander Dracka

Alexander Dracka

  • 0
  • 16
  • 13.4k
Aug 17 2013 11:48 AM
yes and in this method I need put the filepath into TextBox
0
Iftikar Hussain

Iftikar Hussain

  • 0
  • 12.6k
  • 449.2k
Aug 17 2013 11:09 AM
you want to call javascript method after selecting the file?
0
Alexander Dracka

Alexander Dracka

  • 0
  • 16
  • 13.4k
Aug 17 2013 10:55 AM
well i do correct path but I dont know how call this code. I need call it onchange evetn of FileUpload but somthing dont work properly. i got thisFileUploader but dont calls Metoda 
<asp:FileUpload ID="FileUpload2" runat="server" Width="317px" 

style="displaynone" onchange="Metoda"/>

0
Iftikar Hussain

Iftikar Hussain

  • 0
  • 12.6k
  • 449.2k
Aug 17 2013 10:49 AM
Hi,
        I bit confuse about your query. By using the above code you are not getting complete path in your textbox?

Regards,
Iftikar