By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,997 Members | 1,270 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,997 IT Pros & Developers. It's quick & easy.

Writing a value to a <input type="file" name="filebox"> using Javascript

P: 2
This one has really got me!

Im trying to write a value to a input type="file" form element using client-side javascript.

I have tried the obvious, simply writing:
<input type="file" name="filebox" value="myvalue">
..doesnt work (ok I sound like a newbie)

I have tried:
document.formUpload.filebox.value = "myvalue"
:(

also tried:
document.write('<input type="file" name="filebox" value="myvalue">');
:(

Does anybody know a way to do this?!

Many thanks for your time in advance!!!
Dec 27 '07 #1
Share this Question
Share on Google+
3 Replies


acoder
Expert Mod 15k+
P: 16,027
Welcome to TSDN!

You can't set the default filename with JavaScript. You have to set it by selecting a file using the browse button, otherwise it could be a security risk - see this link.
Dec 27 '07 #2

P: 2
Thanks for your reply. Its a pitty it is not possible with Javascript.

Is there a way to convert a normal input type="text" to a type="file"?

In this app. a user has already entered the file (via 'browse') but I need to check a few of the other fields against values in a DB, then.. if those checks are 'false', only then do I want to upload the file they initially entered. (the upload scripts are from a free resource, I do not write my own code at that level :S ). So I have carried the path value that was initailly entered to another form, and here I want to automatically enter the file value, and onload submit the form.

I am using classic asp, and client-side javascript.

Any ideas on how I can achieve this?

Thanks again for your time!
Dec 28 '07 #3

acoder
Expert Mod 15k+
P: 16,027
Is there a way to convert a normal input type="text" to a type="file"?
Yes, by changing the type property to 'file', e.g.
Expand|Select|Wrap|Line Numbers
  1. var field = document.getElementById("IDofTextField");
  2. field.type='file';
In this app. a user has already entered the file (via 'browse') but I need to check a few of the other fields against values in a DB, then.. if those checks are 'false', only then do I want to upload the file they initially entered. (the upload scripts are from a free resource, I do not write my own code at that level :S ). So I have carried the path value that was initailly entered to another form, and here I want to automatically enter the file value, and onload submit the form.
You can make the checks with ASP and if the checks are true, just discard the file, otherwise upload it.
Dec 28 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.