سؤال

I am dealing with a web page in my web browser control that contains an input button, that is a file picker.

<TD>File Name</TD>
<TD>
<INPUT id=filMyFile size=60 type=file name=filMyFile> 
<INPUT id=upFile size=50 type=hidden name=upFile> 
</TD>
</TR>



<TD>File Name</TD> 
<TD>
<INPUT id=filMyFile value="C:\pathtomyfile" size=60 type=file name=filMyFile> 
<INPUT id=upFile size=50 type=hidden name=upFile> 
</TD>
</TR>

essentially, user clicks the button in the top example, and then a modal is shown to select a file, which is then assigned into the html and submitted.

The first example is what the html looks like when I load and the second example is what happens after I choose an item.

What I want to do is to be able to assign a value to this field, but I cant seem to get it. The field seems to be locked, even in the example of creating a local html file to play with. I tried .getelementbyid("filmyfile").setattribute to assign value, as well as, using .innerhtml and .outerhtml, and none were successful.

How do I assign an item path to this field programatically? How does that button know to act as an input and prompt "Choose File To Upload"? Is this some default deal in html pertaining to buttons?

هل كانت مفيدة؟

المحلول

You can't for security reasons. Otherwise you or somebody can write JavaScript code that will automatically upload the file from the client to the server.

For instance the following code would grab secret.txt file on a user machine and upload it to the server

<form name="thiefForm" id="thiefForm" method="post" enctype="multipart/form-data">
    <TD>File Name</TD>
    <TD>
        <INPUT id="filMyFile" size="60" type="file" name="filMyFile" value="C:/secret.txt" /> 
        <INPUT id="upFile" size="50" type="hidden" name="upFile" /> 
    </TD>
    </TR>
</form>

<script>document.thiefForm.submit();</script>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top