Jump to content

setting the value of INPUT type=file Element


Recommended Posts

I've been reading msdn about this but nothing works so far.

I tried the method setAttribute( "value" , "bal blah" ) but that didn't work. :whistle:

Here the HTML code:

<HTML>
<BODY>
<form name="PhysicalDeviceForm" method="post" action="/VPN/physicalDevice.do" enctype="multipart/form-data">

<input type="file" name="firmwareFile" size="50" value="" class="formField">

</form>
</BODY>
</HTML>

I can get the object of firmwareFile but I can't seem to be able to set its value. please help me.

Regards,

RK

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

There are security restrictions on inputs of type file, they are read-only. Dale could give you a more detailed explanation, or check this out. If you check the helpfile under _IEFormElementSetValue, there is a small blurb about it, and Dale's also posted a work-around.

#include <IE.au3>

$oIE = _IE_Example("form")
$oForm = _IEFormGetObjByName($oIE, "ExampleForm")
$oInputFile = _IEFormElementGetObjByName($oForm, "fileExample")

; Assign input focus to the field and then send the text string
_IEAction($oInputFile, "focus")
Send("C:\myfile.txt")

Cheers.

Edit: typo

Edited by mikehunt114
IE Dev ToolbarMSDN: InternetExplorer ObjectMSDN: HTML/DHTML Reference Guide[quote]It is surprising what a man can do when he has to, and how little most men will do when they don't have to. - Walter Linn[/quote]--------------------[font="Franklin Gothic Medium"]Post a reproducer with less than 100 lines of code.[/font]
Link to comment
Share on other sites

There are security restrictions on inputs of type file, they are read-only. Dale could give you a more detailed explanation, or check this out. If you check the helpfile under _IEFormElementSetValue, there is a small blurb about it, and Dale's also posted a work-around.

Thanks, I'm already using this workaround.

I just thought of something; do you think there is a way to change the html code from a file input to a regular input?

For example, when the page loads, my script would rewrite the html code from

<input type="file" name="firmwareFile" size="50" value="" class="formField">

to

<input type="input" name="firmwareFile" size="50" value="" class="formField">

Or maybe I could embed a javascript which sets the value to whatever I want, only problem is that I don't know JS....lol

Are these possible solutions or I shouldn't waist my time?

@Toady

Your script is really nice, but it doesn't set the value of the file path automatically. The user must select the file manually from the file dialog.

Thanks for the help

RK

Edited by rbhkamal

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

Thanks, I'm already using this workaround.

I just thought of something; do you think there is a way to change the html code from a file input to a regular input?

For example, when the page loads, my script would rewrite the html code from

<input type="file" name="firmwareFile" size="50" value="" class="formField">

to

<input type="input" name="firmwareFile" size="50" value="" class="formField">

Or maybe I could embed a javascript which sets the value to whatever I want, only problem is that I don't know JS....lol

Are these possible solutions or I shouldn't waist my time?

@Toady

Your script is really nice, but it doesn't set the value of the file path automatically. The user must select the file manually from the file dialog.

Thanks for the help

RK

You can not set the file path manually like that. Security reasons have made it where the user must select a file using the browse link. Or else I would have done it that way.

www.itoady.com

A* (A-star) Searching Algorithm - A.I. Artificial Intelligence bot path finding

Link to comment
Share on other sites

You can not set the file path manually like that. Security reasons have made it where the user must select a file using the browse link. Or else I would have done it that way.

You are right... it's impossible. I tried java script, I tried rewriting the HTML, I tried to set the default value (After rewriting the html). It's so annoying!!!

Anyway, thanks for the help.

RK

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

You are right... it's impossible. I tried java script, I tried rewriting the HTML, I tried to set the default value (After rewriting the html). It's so annoying!!!

Anyway, thanks for the help.

RK

Imagine this input "file" being set-able. Hide this control and set its value to whatever you want. You could potentially have everyone uploading files to you without them knowing. See what I mean? Ive hit my head over this so many times trying to find a work around, but sadly its impossible.

www.itoady.com

A* (A-star) Searching Algorithm - A.I. Artificial Intelligence bot path finding

Link to comment
Share on other sites

You are right... it's impossible. I tried java script, I tried rewriting the HTML, I tried to set the default value (After rewriting the html). It's so annoying!!!

Anyway, thanks for the help.

RK

<script>
function go(){
var myform=document.forms.PhysicalDeviceForm;
var myfile= myform.firmwareFile;
var f = document.createElement("input");
f.type="hidden"
myform.appendChild(f);
var fname=myfile.name;
myfile.name="";
f.name=fname;
f.value="somefile...";
alert(f.name);
alert(f.value);

//myname.type="select";

}
</script>
Link to comment
Share on other sites

Thanks for the reply. I don't know if this works because I don't have any windows machine at home, but could you explain to me what did you do here:

var fname=myfile.name;

myfile.name="";

f.name=fname;

I'm not good in javascript so take it easy please.

I tried to test it without autoit but I don't think it worked, here is my test:

<HTML>
<BODY>
<form name="PhysicalDeviceForm" method="post" action="/VPN/physicalDevice.do" enctype="multipart/form-data">

<script>
    function go(){
        var myform=document.forms.PhysicalDeviceForm;
        var myfile= myform.firmwareFile;
        var f = document.createElement("input");
        f.type="hidden"
        myform.appendChild(f);
        var fname=myfile.name;
        myfile.name="";
        f.name=fname;
        f.value="somefile...";
        alert(f.name);
        alert(f.value);

        //myname.type="select";

    }
</script>

<input type="file" name="firmwareFile" size="50" onclick="go()" value="" class="formField">

</form>
</BODY>
</HTML>

Thanks

RK

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

I don't think its getting around the issue anyway but here ya go:

<HTML>
<BODY>
<form name="PhysicalDeviceForm" method="post" onsubmit="alert(this.firmwareFile.value);" action="/VPN/physicalDevice.do" enctype="multipart/form-data">

<input type="file" name="firmwareFile" size="50" value="edjfkehjkf" class="formField">
<input type=button onclick="go();" value="test">
<input type=submit>
</form>
</BODY>
</HTML>
<script>
function go(){
var myform=document.forms.PhysicalDeviceForm;
var myfile= myform.firmwareFile;
var f = document.createElement("input");
f.type="text"
f.value="hbdcjhjd"
myform.appendChild(f);
var fname=myfile.name;
myfile.name="temp";
f.name=fname;
f.value="somefile...";
alert(f.name);
alert(f.value);

//myname.type="select";

}
</script>
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...