As a means to circumvent security issues, the HTML file input element doesn't allow developers to change the value of the element using normal scripting methods. However, the file input is a valuable commodity that can add value to intranet applications. And when you can't control the value of this element, it ties your hands when it comes to implementing quick and easy solutions.

In this article, I provide simple code that will give you this functionality, and discuss why you can benefit from it. (Note: I created this sample with Visual Basic 6.0 and tested it in Internet Explorer 6.0.)

If you work with .NET, then you're probably aware of the virtues of the HttpPostedFile class. It provides you with the binary data that composes the posted file, and it gives you valuable information concerning the file such as the ContentType (the MIME type) and the posted file's FileName. The SaveAs() method encapsulates the code needed to convert the data to a file. All of this information can come in quite handy when you reference the document for retrieval. It's easy to set the Content-type header and the file name if you store this information someplace where you can reference it again such as a database. The best benefit you achieve is the time savings you get developing this solution on the server side.

Now, if all you need to do is implement a basic browse and pick solution for adding files, then there's no need to add functionality to the file input element. However, if you want to provide both a browse button and another function, such as an image scanning solution, you need to be able to set the value of the file input element to the name of the temporary file of the scanned image. In this case, if you're using Internet Explorer, you're probably hosting an ActiveX or Web Form control to provide the scanning functionality. You can exploit this control to set the value of the file input element.

The security of the file input element is set in the control, so providing a secure method for setting the value isn't available. However, with Visual Basic 6.0, you can use the SendKeys() function to "type" in the value. (I chose VB because I'm familiar with the language, and the VB runtime exists on most Windows 2000 and Windows XP computers. To use .NET technologies, you must install the .NET Framework on the client computer.)

In VB, you can create a public function that you would pass the ID of the file input element and the name of the file:

Public Function SetInputValue(id As String, newValue As String) As String
On Error Resume Next
   Dim doc As MSHTML.HTMLDocument
   Dim inp As MSHTML.HTMLInputFileElement
   Set doc = UserControl.Parent
   Set inp = doc.All.Item(id)
   inp.focus
   SendKeys newValue, False
   SetInputValue = inp.Value
   Set inp = Nothing
   Set doc = Nothing
End Function

This very simple approach sets the focus on the HTML control and executes the key sequence. You do need to add a reference to the Microsoft HTML Object Library in your project.

In order to run this sample, your control must be allowed to execute within the browser. You can also choose to implement the IObjectSafety interface and/or use a digitally signed installation package.

You can test the code with this HTML:

<html>
<head>
<script language="JavaScript">
function btn_onclick() {
    FileInput1.SetInputValue("file1", "c:\\test.txt");
}
</script>
</head>
<body>
<input type="file" id="file1">
<OBJECT id=FileInput1 classid="clsid:759F074C-8D49-40E0-B6FC-5AC1169BD29A"
 VIEWASTEXT>
<span style="color:red">ActiveX control could not load. Check your security
 settings.</span>
</OBJECT>
<button id="btn1" onclick="btn_onclick()">Click Me</button>
</body>
</html>

Comments

1

James Villela - 23/12/04

Thanks for this article. I was wasting time trying to figure out why I couldn't change the value directly. Now I know it designed that way and we must use sendkeys. I was hoping to have my program run in the background but that looks impossible.

» Report offensive content

2

Sutirtha Saha - 19/02/05

Is there a jscript equivalent for this routine for pushing back characters via Sendkeys

» Report offensive content

3

Sutirtha Saha - 19/02/05

Is there a jscript equivalent for this routine for pushing back characters via Sendkeys

» Report offensive content

4

khodadad Nejadkoorki - 13/03/05

Hi,

Thanx for your paper, I used your codes (ActiveX and test page) but I meet some problem that the activex function (SetInputValue) does not set the value of file upload element, please help me ASAP.

best

» Report offensive content

5

christos - 27/07/06

This code doesnt work in xp sp2 with IE6 and latest service packs. when you try to submit the form containing the file control you get an Access denied error !

» Report offensive content

6

Srinivasan - 21/11/08

Thans for this Article. Really it would be helpful for my requirement. I need some quite clear about the same.

Thanks in Advance

» Report offensive content

Leave a comment

You must read and type the 6 chars within 0..9 and A..F

* indicates mandatory fields.

6

Srinivasan - 21/11/08

Thans for this Article. Really it would be helpful for my requirement. I need some quite clear about the same. Thanks in ... more

5

christos - 27/07/06

This code doesnt work in xp sp2 with IE6 and latest service packs. when you try to submit the form containing ... more

4

khodadad Nejadkoorki - 13/03/05

Hi, Thanx for your paper, I used your codes (ActiveX and test page) but I meet some problem that the activex ... more

Log in


Sign up | Forgot your password?

  • Staff Microsoft shows off IE9 preview

    This week, highlights from Microsoft's MIX10 conference and more in the Roundup. Read more »

    -- posted by Staff

  • Chris Duckett IE9's H.264 vote killed Ogg

    In a split decision by the judges, the winner of the W3C/WHATWG video codec consensus is H.264, taking home the future of video playback on the internet while loser Ogg goes home with nothing but thoughts of what might have been. Read more »

    -- posted by Chris Duckett

  • Staff Google launches Apps Marketplace

    Google launches and app store, while Mozilla plans to re-write its open-source license. More of this week's news in the Roundup. Read more »

    -- posted by Staff

What's on?

  • Optus Deal

    Broadband + home phone + PlayStation®3 in a single package price!