Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
Javascript How-To Ask your "How do I do this with Javascript?" questions here.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old November 4th, 2006, 04:03 AM
Authorized User
 
Join Date: Oct 2006
Posts: 11
Thanks: 0
Thanked 0 Times in 0 Posts
Default setting the value of a file object

I have a control on me screen of type <FILE>. I want to set some value to that object from the database. I tried the following code using html.


<html >
<head>
<script type="text/javascript">
  function fun()
  {
    document.getElementById('j').value="hi"
    alert(document.getElementById('j').value);
  }
</script>
</head>

<body>
<input type="file" value="hello" id="j" />
<input type="button" onclick="alert(document.getElementById('j').value) ;"
<input type="button" onclick="fun();"
</body>
</html>
but none of the above showed proper values for alert. it was showing null.

If I'm entering some values into the file field manalyy then proper alert is coming.
If I change the file type to text, then it is setting the value in the textfield with the value in the value attribute.

So my question is how to I set the value into the <FILE> type through the code implicitly.
 
Old November 4th, 2006, 04:32 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 425
Thanks: 0
Thanked 3 Times in 3 Posts
Default

You would probably get a better answer in the JavaScript forum, http://p2p.wrox.com/javascript-85/ , but I don't think you can do what you want, not reliably anyway. See http://www.cs.tut.fi/~jkorpela/forms/file.html#value .

--
http://yupapa.com
 
Old November 6th, 2006, 10:04 AM
richard.york's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 1,706
Thanks: 0
Thanked 6 Times in 6 Posts
Default

You can set the value of a file input if you have elevated security settings (as is the case on an intranet).

If this isn't an intranet site, you'll have to get the user to trust your site to do that. In IE, that means granting your site elevated privileges.

I'm not sure how it's done in IE from the standpoint of code, it may just be a matter of trusting the site. In Firefox, you have to have a signed script. Without a signed script, you can enable a special security dialogue, which once allowed will allow you to set the value of file objects.

To do that without a signed script, following is the process:

Add the following to prefs.js (located in Firefox profile folder)
Code:
user_pref("signed.applets.codebase_principal_support", true);
Then your script must ask the user for permission, I think both of these are required.
Code:
        try
        {
            netscape.security.PrivilegeManager.enablePrivilege('UniversalXPConnect');
        }
        catch($err)
        {
            var $privileges = false;
            alert('Error: Unable to perform file upload. App was denied elevated privileges to do so.');
        }
AND

Code:
        try
        {
            netscape.security.PrivilegeManager.enablePrivilege('UniversalFileRead');
        }
        catch($err)
        {
            var $privileges = false;
            alert('Error: Unable to perform file upload. App was denied elevated privileges to do so.');
        }
This technique enables the security prompt on your computer alone, which is good for testing. To get the prompt on everyone else's, you must have a signed script, or you must manually install that line in their prefs.js file.

So, as you can see there are a lot of security hurdles, and rightly so. If you could just arbitrarily set the value of the file upload, you could upload files from the user's computer without their knowing, and that would be bad.

HTH!

Regards,
Rich

--
Author,
Beginning CSS: Cascading Style Sheets For Web Design
CSS Instant Results

http://www.catb.org/~esr/faqs/smart-questions.html
 
Old November 6th, 2006, 11:13 AM
joefawcett's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 3,074
Thanks: 1
Thanked 38 Times in 37 Posts
Default

IE will let you set the value in code but you won't be able to upload the file when the form is posted.

--

Joe (Microsoft MVP - XML)





Similar Threads
Thread Thread Starter Forum Replies Last Post
Default values when setting up an object data sour mikener ASP.NET 2.0 Professional 0 November 3rd, 2008 09:12 AM
Setting of web config file epkgupta General .NET 0 July 9th, 2008 09:32 AM
setting the value of a file object swarnap Javascript 2 November 6th, 2006 10:10 AM
Setting stylesheet for dynamically created object tgopal Javascript 2 September 6th, 2004 11:47 PM
File security setting wwshi Classic ASP Basics 0 March 30th, 2004 10:20 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.