Browse button without file upload in html

后端 未结 1 1356
半阙折子戏
半阙折子戏 2021-01-22 23:40

I have script, which takes path and copy some files into it. What I want is: I want to have browse button and when user clicks it, he will be able to just select folder he wants

相关标签:
1条回答
  • 2021-01-23 00:04

    This just isn't possible because of security issues that would be exposed by this functionality, i.e. being able to log the user's file directories. When a user browses for a file, the dialog is created by the browser and none of this information is relayed back, only the contents of the selected file.

    The only solution I can think of without hitting up ActiveX or similar is to force the user to select a file from within their chosen directory and upload it. This has obvious drawbacks in that they may not want their files to be uploaded and also that they may not have any files within their desired folder. However, assuming that a) they have a file and b) that they don't care about how you deal with that file you can:

    <form name="folderForm">
        <input type=file name="file">
        <input type=text name="folderLocation">
        <input type=button value="Get Folder" onclick="javascript:GetFolder();">
    </form>
    
    function GetFolder() {
        document.folderForm.folderLocation.value=document.folderForm.file.value;
    }
    

    Then on the server side you can parse the value of folderLocation to remove the file name after the last / or do this in GetFolder() if you wish.

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