ASP.NET: Get file path on client's machine (Any possible solutions/work arounds?)

前端 未结 4 491
难免孤独
难免孤独 2020-12-22 12:55

I have a file upload control on .aspx page where I am extracting basic file information. The requirement is that I need to save absolute path for selected file from client\'

相关标签:
4条回答
  • 2020-12-22 13:14

    Browser send file without full file path, so to retrieve file path you do it on client side via javascript function:

    <script type="text/javascript">
    function OnSubmitHandler(myForm)
    {
       var fileUpload = document.getElementById('<% = FileUpload1.ClientID %>');
    
    
       myForm.action =  myForm.action + "?FilePath=" + fileUpload.value;
    
    
       return true;
    
    
    }
    </script>
    
    
    <form id="Form1" method="post" runat="server" onsubmit="return OnSubmitHandler(this);">
    ...
    <asp:FileUpload id="FileUpload1" ....  />
    
    
    ...
    </form>
    

    If you don't want to send a file path via query string, you can create hidden form field, so file path will be sent via post.

    0 讨论(0)
  • 2020-12-22 13:17

    You can't really do anything reasonable about this restriction.

    Any fully trusted code on users' machine can do that. So your goal is to convince user to install something that will provide you with the information.

    For Windows (all require instalation of some sort):

    • ActiveX controls
    • Native EXE
    • Locally installed managed EXE
    • locally installed HTA for IE only

    I believe almost all platforms also have ways to to so... with similarly painful installation requirements.

    0 讨论(0)
  • 2020-12-22 13:19

    You can't get full path of the file. For security purposes, the browser will never post the full file's path.

    Try to use below code in java script.

    lbltext.Text = FileUpload1.PostedFile.FileName 
    
    0 讨论(0)
  • 2020-12-22 13:32

    You can't get client machine path. For security purposes, the browser will never post the full file's path.

    0 讨论(0)
提交回复
热议问题