Passing path to uploaded file from HTML5 drag & drop to input field

前端 未结 3 1055
死守一世寂寞
死守一世寂寞 2020-12-29 09:36

I\'m working on an application (in Node.js, which is irrelevant for this case) which allows the user to upload an image. It works fine using a form with an input (type=\"fil

相关标签:
3条回答
  • 2020-12-29 10:15

    I find that the hidden field set in reader.onload (see answer by @challet) is not set when acccessed in code behind. I am using asp.net and a WebForms project. To access the hidden fields I have to prepend MainContent_ to the field names. aspx code is below

    
    <asp:Content ID="Content3" ContentPlaceHolderID="MainContent" runat="server">
    ...
    <script type="text/javascript">
        function dropHandler(ev) {
            alert("File(s) dropped");
            // Prevent default behavior (Prevent file from being opened)
            ev.preventDefault();
            //alert("Default prevented");
            if (ev.dataTransfer.items) {
                if (ev.dataTransfer.items.length > 1) {
                    alert("Only single files can be dragged and dropped into Caption Pro Web");
                    return;
                }
                // If dropped items aren't files, reject them
                if (ev.dataTransfer.items[0].kind === 'file') {
                    var file = ev.dataTransfer.items[0].getAsFile();                    
                    document.getElementById("MainContent_DroppedFileName").value = ev.dataTransfer.items[0].name
                    reader = new FileReader();
                    reader.onload = function (event) {
                        document.getElementById('MainContent_DroppedFileContent').value = event.target.result;
                    };
                    reader.readAsDataURL(ev.dataTransfer.items[0]);               
                }
            } else {
                // Use DataTransfer interface to access the file(s)
                 if (ev.dataTransfer.files.length > 1) {
                    alert("Only single files can be dragged and dropped into Caption Pro Web");
                    return;
                }           
                document.getElementById("MainContent_DroppedFileName").value = ev.dataTransfer.files[0].name
                document.getElementById("MainContent_DroppedFileContent").value = "Test";
    
                reader = new FileReader();
                reader.onload = function (event) {
                     document.getElementById("MainContent_DroppedFileContent").value = event.target.result;
                };
                reader.readAsDataURL(ev.dataTransfer.files[0]);           
            }
    
            document.getElementById('<%=btnDrop.ClientID %>').click();
    
        }
    </script>
    
    ...
    
        <div id="drop_zone" ondrop="dropHandler(event);" ondragover="dragOverHandler(event);">
        <p>Drag image to this Drop Zone ...</p>
        </div> 
        <asp:HiddenField ID="DroppedFileName" runat="server" />
        <asp:HiddenField ID="DroppedFileContent" runat="server" />
    ...
     </asp:Content>
    

    I access the hidden fields from c# as shown below

    protected void btnDrop_Click(object sender, EventArgs e)
             {
                 string FileName = DroppedFileName.Value;
                 string FileContent = DroppedFileContent.Value;
             }
    

    If I use Internet Explorer as the target browser (not running VS as Admin as this disables drag/drop!) and set a breakpoint in the reader.onload() function the hidden field DroppedFileContent contains the encoded file content, but when I try to access it from btnDrop_Click it only contains "Test" as set before reader.onload() and does not contain the encoded file content. The field DroppedFileNam.Value is as set in the Javascript.

    0 讨论(0)
  • 2020-12-29 10:20

    You cannot use the file input to add the file data. Nevertheless, what you can do (among other technics) is to use the base64 (natively available through the reader.onload event as event.target.result, when using readAsDataURL method) encoded data and put it into an hidden field :

    html

    <article>
        <div id='holder'>
            <p id='status'>File API and FileReader API not supported</p>
        </div>
    </article> 
    
    <form method='post' enctype='multipart/form-data' action='/file-upload'>
            <input type='file' name='thumbnail' />
            <input type='hidden' name='base64data' />
            <input type='submit' formenctype='application/x-www-form-urlencoded' />
    </form>
    

    js

    reader = new FileReader();
    reader.onload = function (event) {
        document.getElementById('base64data').setAttribute('value', event.target.result);
    };
    reader.readAsDataURL(file);
    

    From the server side you'll be able to get the base64 encoded data from the file, just decode it and use it as you want.

    While submitting the form, you could also change the "enctype" attribute (done through the formenctype attribute) and remove the basic html file input, since the data will be post in a text field.

    0 讨论(0)
  • 2020-12-29 10:26

    It is impossible to know the path of the field for security purposes. With drag and drop you must have it upload independently of the main form. Look here for an example: http://www.sitepoint.com/html5-file-drag-and-drop/

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