To place XUpload on an HTML page, an <OBJECT> tag should be used with the CLASSID attribute
set to XUpload's unique class ID. You must also specify
a path to the location of the xupload.ocx file relative to the current HTML page (and optionally
its current version) via the CODEBASE
attribute. Other useful attributes are ID to identify the control in client-side VB or Java script,
and WIDTH and HEIGHT to specify the control's size on the page.
XUpload is configured to do useful work via a set of required and optional <PARAM> tags which are placed
between the opening and closing <OBJECT> tags. The two required
parameters are Server and Script which together specify the location of a server-side upload
script which captures the uploaded files. In all code samples of this user manual, the
Server parameter is set to localhost. In a real-life application, that has to be replaced
by an actual server address, such www.server.com. An IP address can also be used.
HTML:
<OBJECT WIDTH=500 HEIGHT=200
ID="UploadCtl"
CLASSID="CLSID:E87F6C8E-16C0-11D3-BEF7-009027438003"
CODEBASE="XUpload.ocx">
<param name="server" value="localhost">
<param name="script" value="/xupload/01_simple_upload.aspx">
</OBJECT>
|
An upload script usually captures the uploaded files and places them in a folder or database on the server.
In classic ASP, a server-side upload component such as AspUpload
is required. ASP.NET provides its own built-in upload functionality via the HttpPostedFile object.
The following code samples are simple upload scripts written for the ASP and ASP.NET environments.
To run the former, you must also have AspUpload installed on the server. Both code samples place the uploaded files
in the c:\upload directory on the machine they are running on, so make sure this directory exists
before running the samples or change the scripts to point to some other, existing directory.
Server-Side VBScript:
Set Upload = Server.CreateObject("Persits.Upload")
Upload.IgnoreNoPost = True
Count = Upload.Save( "c:\upload" )
Response.Write Count & " files(s) uploaded:" & chr(13) & chr(10) & chr(13) & chr(10)
For Each File in Upload.Files
Response.Write File.Path & chr(13) & chr(10)
Next
|
C#:
<%@ import Namespace="System.IO" %>
<script runat="server" language="C#">
void Page_Load( Object sender, EventArgs e)
{
if( Request.ContentType.IndexOf( "multipart/form-data" ) < 0 )
return;
string strUploadPath = "c:\\upload";
Response.Write( Request.Files.Count.ToString() + " file(s) have been uploaded:\r\n\r\n" );
for( int i = 0; i < Request.Files.Count; i++ )
{
HttpPostedFile objFile = Request.Files[i];
String strPath = strUploadPath + "\\" + Path.GetFileName( objFile.FileName );
objFile.SaveAs( strPath );
Response.Write( strPath + "\r\n" );
}
}
</script>
|
Click the links below to run this code sample:
http://localhost/xupload/01_simple.asp
http://localhost/xupload/01_simple.aspx
Unless the XUpload control was previously installed on your machine,
you should see a dialog box similar to this one:
The file XUpload.ocx is digitally signed to provide proof to the user that the control he is about to
install on his machine really came from Persits Software, Inc., and that it was not
tempered with during transmission. Click "Yes" to have the browser install the control
on your computer.
You should now be able to see the control inside your browser. It looks like a standard Windows list view
control with two columns, Files and Size. Right-click anywhere inside the control. A pop-up menu appears
with four menu items, "Select Files", "Select Folder", "Remove All" and "Upload".
The Remove All and Upload items are grayed out as there are no files selected.
Choose Select Files to invoke the standard "Select Files" dialog box. Choose a few files and click Ok.
The selected files appear in XUpload's list control with their respective icons and size information.
You can now remove any or all files from the list by highlighting the appropriate items,
right-clicking inside the control and choosing Remove or Remove All.
We can also sort the items alphabetically or by size by clicking on the Files or Size headers, respectively.
Now select "Upload" from the pop-up window. A progress dialog box appears.
Once the upload is complete, a message box titled "Reply from the Server" pops up:
The text inside the message box came directly from the server and was generated by our upload script.
Alternatively, XUpload can redirect the browser to another page upon the completion of an upload,
as described in Chapter 2.