/    Sign up×
Community /Pin to ProfileBookmark

JavaScript for "Choose File"

I want to fill the HTML form with JavaScript. The following JavaScript code

document.forms[‘theForm’][‘city’].value = ‘Ottawa’

fills the HTML form OK as follows.

<input type=”text” name=”city” value=”Ottawa”>

However, the following JavaScript

document.forms[‘theForm’][‘uploadfile’].value = ‘/Users/gss/Desktop/ToDo.txt’

does not fill the form to upload a file next to question “Choose File”. What I want is the following, but the “value” part is not accepted for some reason.

<input type=’file’ name=’uploadfile’ value=’/Users/gss/Desktop/ToDo.txt’/>

Even if I directly hard-code the “value” part in HTML itself, the web browser does not show it. Could anyone tell me why and how to make the JavaScript work? Thanks in advance!!!

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@WebWarriorNov 16.2010 — As described in http://www.w3schools.com/jsref/dom_obj_fileupload.asp

you can only "get" the [I]value[/I] property of the[I] input file[/I]. You cannot "set" it. You can however set/get [I]defaultValue[/I] property.
Copy linkTweet thisAlerts:
@GoSimonauthorNov 16.2010 — As described in http://www.w3schools.com/jsref/dom_obj_fileupload.asp

you can only "get" the [I]value[/I] property of the[I] input file[/I]. You cannot "set" it. You can however set/get [I]defaultValue[/I] property.[/QUOTE]


defaultValue Sets or returns the initial value of the FileUpload object

However,

<input type='file' name='uploadfile' defaultValue='/Users/gss/Desktop/ToDo.txt'/>

still says "no file selected", and

document.forms['theForm']['uploadfile'].defaultValue = '/Users/gss/Desktop/ToDo.txt'

has no effect at all. What is going on? My web browser is Safari.
Copy linkTweet thisAlerts:
@WebWarriorNov 16.2010 — Actually, I believe there is no standard way to populate the file input field. I didn't think of this much, since I never had to use it. Today however, after running a few tests and googling this issue a bit, I will have to admit so. This is really a security concern on the part of the browsers and there isn't much that can be done here really.
Copy linkTweet thisAlerts:
@rnd_meNov 16.2010 — the user must click and choose a file to populate the input, despite what any attribs you set say...

this is a basic security must: imaging if someone could pre-populate config.sys, htaccess, bookmarks.html, or the like...
×

Success!

Help @GoSimon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...