3

I want to auto select a file through
File input tag < input type=file > via java script ?

I know this is security issue and not allowed normally.

But is this allowed through extensions or in any form in any browser,

because i m working on a automation project
which requires posting of a form with multiple values has upload feature which uploads file first and then posts form(kijiji.ca)

and need this to be done.

need not to be browser specific any browser which have automation capability like chrome, Fire fox

Further reuquirement

Hi Wladimir, i got it working as Wladimir suggested in Extension scope.

Can u tell me if i need to auto select multiple files what value should i set.

I tried setting this

input.value = '"D:\Temp\step1.jpg" "D:\Temp\step2.jpg" "D:\Temp\step3.jpg"' 

but it don't accept it. Though i can set it to single image and it takes it..

thanks..

2
  • 1
    Hi @wladimirpalant, just waiting for your answer on it.. Commented May 3, 2014 at 8:11
  • 1
    Making this a conversation between you and another person really doesn't work two years later. Please update your question to show what you have done so far. But "" isn't legal syntax; I think you want "\"filename"\". Commented May 3, 2014 at 11:21

1 Answer 1

4

Firefox extensions are allowed to set input.value property so something like this will work:

input.value = "c:\\autoexec.bat"; input.form.submit(); 

However, when using Add-on SDK a content script will likely not have the necessary privileges (needs to be tested). In this case one would need to use chrome authority and access the web page directly rather than via a content script.

As to Chrome, extensions there don't have access to the file system - so the security restrictions that apply to web pages are applying to extensions as well. Allowing extensions to set the value of a file upload widget would be a security issue there. So I'm pretty sure that it isn't possible.

Sign up to request clarification or add additional context in comments.

8 Comments

hey i tried it on FF 3.6 not working. i have this < input id="s" type="file" > when i set document.getElementById('s').value="E:\image1.jpg"; and check document.getElementById('s').files it returns 0 files
@SohanChotia: You shouldn't be testing anything in Firefox 3.6 - it's outdated and no longer supported by Mozilla. In particular, File API was still very new when Firefox 3.6 was released. In Firefox 12 this works, at least when you run it from a privileged context. input.files.length gives me 1 file and file name is correct.
What privilege should i do please can u give me the code.Thnx for ur help. I m running this code from overlay.js. Also i installed FF 12 now window.addEventListener("load", function () { myExtension.init(); alert('ss'); }, true); is not working, no alert displayed????
Hi i made it working files showing 1, but one problem there is onchange event attached to this input which is not firing. how to make it fire.
@SohanChotia: The change event only fires if the user changes input value. In your case you should dispatch that event manually (dispatchEvent example). Feel free to accept the answer if it helped you: stackoverflow.com/faq#howtoask
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.