Presetting Files for Upload
You may at times run into situations where you do not want the user to be able to
set which files he or she will be uploading. For obvious reasons, this is not allowed
with a purely server-based applications, as the security implications would be rather
daunting. However, you can preset files with a client-side component.
Presetting Files with SoftArtisans XFile
For this example, you only need to create one file to post to the standard FormResp.asp.
Paste the following code into a standard HTML file:
The Client Code |
---|
<html> <body>
<script language="vbscript">
Sub UploadButton_OnClick() XFRequest.CurrentURL = "http://localhost/saxfilesamples/formresp2.asp" XFRequest.AddFile "C:\Boot.ini", "FileName1" XFRequest.Start ResponseText.InnerHTML = XFRequest.Response Document.GetElementById("UploadButton").Disabled = True End Sub
</script>
<div align="center"> <h2>IE Based XFile Form Data and Multi-File "Post" Sample</h2> <br /> To upload the preset files click the "Upload!" button. <br />
<input type="button" id="UploadButton" name="UploadButton"
value="Upload!" />
</div> <span id="ResponseText"></span>
<object classid="ClSID:C3A57B60-C117-11D2-BD9B-00105A0A7E89" codebase="saxfile.cab" id="XFRequest"> </object>
</body> </html>
|
The code for this is fairly easy to understand. The object tag at the end creates
an instance of
XFile's XFRequest object.The codebase parameter is necessary for those users who have not yet installed
XFile.This will obviously not be a real problem if you are just running the sample on
a local machine, but it is essential to use this parameter if you are using
XFilein a production environment. The ID parameter, which is set to "XFRequest", is necessary
to allow us to refer to the object in the rest of our code.
We named the button in this example 'UploadButton'. Its sole purpose is to provide
us with an "on click" event from which the script can be called.
Let's go through the sub procedure line by line:
- The CurrentURL line sets the URL to where we want to send the data.
- The AddFile line adds a file to be uploaded from the user's hard drive.
- The Start line begins the upload process. Since we are only uploading one small
file here, this process will not take long.
- The ResponseText.InnerHTML line allows us to place the response from FormResp.asp
into the area that we define with the SPAN tag later on. This allows us to see the
results of the upload. If you do not want the user to see the results, it is not
necessary to use this.
- We disable the UploadButton by setting its Disabled property to True.
At this point, you should be able to open the HTML file, press the button, and have
the boot.ini from your hard drive uploaded without having selected it manually.
For more examples, check out the samples provided as part of the
FileUpdistribution. See Programmer's Samples for a list of the contents of the Samples
directory.
Copyright © 2010 SoftArtisans, Inc. All rights reserved.