2.2.1 RedirectURL Parameter
Yet another method that XUpload offers to display server reply information is browser redirection.
When this method is used, the browser opens another web page upon the completion of an upload
that displays the upload results.
To enable redirection, two server-side scripts are necessary: the first one works as a regular
upload script, and the second does nothing but display HTML output generated by the first script.
An additional RedirectURL parameter configures XUpload to perform
the redirection:
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/02_redirect_upload.asp">
<PARAM NAME="RedirectURL" VALUE="http://localhost/xupload/02_redirect_reply.asp">
</OBJECT>
|
The RedirectURL property, if present, instructs XUpload not to display the HTML stream received
from the upload script. Instead, XUpload POSTs the upload results to the URL specified by
the RedirectURL parameter, in the following format:
XUPLOADREPLY=<URL-encoded results>
Therefore, the script 02_redirect_reply.asp can be as simple as this single line of ASP code:
<% Response.Write Request.Form("XUPLOADREPLY") %>
XUpload expects the text data generated by an upload script to be
pure-ASCII or UTF-8 encoded. In classic ASP, we convert possible Unicode characters
in file paths to ASCII using the Server.HtmlEncode method. In ASP.NET, Unicode data
gets UTF-8 encoded automatically, but we do need to include a utf-8 content-type header in the
upload script to have the browser display UTF-8 encoded data correctly.
The scripts ASP and ASP.NET scripts 02_redirect_upload.asp and 02_redirect_upload.aspx
are shown below.
Server-Side VBScript:
<%
Set Upload = Server.CreateObject("Persits.Upload")
Upload.IgnoreNoPost = True
Upload.CodePage = 65001
nCount = Upload.Save("c:\upload")
%>
<h3>Success! <% = nCount %> files uploaded.</h3>
<TABLE CELLSPACING=0 CELLPADDING=2 BORDER=1>
<TR><TH>Path</TH><TH>Size</TH><TH>Content-Type</TH></TR>
<%
For Each File in Upload.Files
%>
<TR><TD><% = Server.HtmlEncode( File.Path ) %></TD>
<TD ALIGN="RIGHT"><% = File.Size %></TD>
<TD><% = File.ContentType %></TD></TR>
<%
Next
%>
</TABLE>
|
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";
TableRow objRow;
TableCell objCell;
for( int i = 0; i < Request.Files.Count; i++ )
{
objRow = new TableRow();
HttpPostedFile objFile = Request.Files[i];
String strPath = strUploadPath + "\\" + Path.GetFileName( objFile.FileName );
objFile.SaveAs( strPath );
objCell = new TableCell();
objCell.Text = strPath;
objRow.Cells.Add( objCell );
objCell = new TableCell();
objCell.Text = objFile.ContentLength.ToString();
objRow.Cells.Add( objCell );
objCell = new TableCell();
objCell.Text = objFile.ContentType;
objRow.Cells.Add( objCell );
objTable.Rows.Add( objRow );
}
}
</script>
<HTML>
<HEAD>
<META http-equiv="Content-Type" content="text/html;charset=utf-8">
</HEAD>
<BODY>
<asp:table id="objTable" runat="server" border="1">
<asp:TableRow>
<asp:TableCell text="Path"/>
<asp:TableCell text="Size"/>
<asp:TableCell text="ContentType"/>
</asp:TableRow>
</asp:table>
</BODY>
</HTML>
|
Click the links below to run this code sample:
http://localhost/xupload/02_redirect.asp
http://localhost/xupload/02_redirect.aspx
By default, the upload results are displayed in the same browser window, and in the same
frame (in case there is a frameset).
Starting with XUpload 3.0, you can specify a different target window or frame via the RedirectTarget
parameter. For example, setting this parameter to the value "_blank"
opens a new browser window:
<PARAM NAME="RedirectTarget" VALUE="_blank">
Other possible values for this parameter are:
- _parent: load the URL into the immediate parent of the document the XUpload control is in;
- _self: load the URL into the same window the XUpload control is in;
- _top: load the URL into the full body of the current window;
- <window_name>: a named HTML frame. If no frame or window exists that matches
the specified target name, a new window is opened.
2.2.2 RedirectURL2 Parameter
The redirect feature described above causes a problem in Internet Explorer 8. The browser
may display the following warning:
As of Version 3.1, XUpload provides an alternative redirect method which does not
cause the above cross-site scripting warning.
The new parameter introduced in version 3.1 is called RedirectURL2.
Unlike RedirectURL, it instructs XUpload to POST the result of the upload script
to the specified URL as is, without any encoding, and does not add the "XUPLOADREPLY=" prefix
in front of the data. It is therefore your responsibility as the application developer
to write the upload script in such a way that it formats the useful output data such as file paths, sizes, etc.
as a list of URL-encoded variables, e.g.
Count=2&path=path1&size=21312&path=path2&size=343534
The URL specified by the RedirectURL2 parameter has to display the data passed from the upload script
in a formatted manner. The following code sample demonstrates one possible way
the upload script and redirect URL can be written:
Upload Script (Server-Side VBScript):
Session.CodePage = 65001
Set Upload = Server.CreateObject("Persits.Upload")
Upload.IgnoreNoPost = True
Upload.CodePage = 65001
nCount = Upload.Save("c:\upload")
Response.Write "Count=" & nCount
For Each File in Upload.Files
Response.Write "&Path=" & Server.UrlEncode( File.Path )
Response.Write "&Size=" & File.Size
Response.Write "&ContentType=" & _
Server.UrlEncode( File.ContentType )
Next
|
Redirect URL: (Server-Side VBScript):
<table border="1" cellspacing="0">
<TR>
<TH>Path</TH><TH>Size</TH><TH>Content-Type</TH>
</TR>
<%
count = Request.Form("path").Count
For i = 1 To count
%>
<TR>
<TD><% = Request.Form("Path")(i)%></TD>
<TD><% = Request.Form("Size")(i)%></TD>
<TD>% = Request.Form("ContentType")(i)%></TD>
</TR>
<%
Next
%>
</table>
|
Upload Script (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( "Count=" + Request.Files.Count.ToString() );
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("&Path="+ Server.UrlEncode(strPath) );
Response.Write("&Size="+ objFile.ContentLength.ToString());
Response.Write("&ContentType=" +
Server.UrlEncode( objFile.ContentType ) );
}
}
</script>
|
Redirect URL (C#):
<script runat="server" language="C#">
void Page_Load( Object sender, EventArgs e)
{
TableRow objRow;
TableCell objCell;
objCount.Text = Request.Form["Count"];
int nCount = int.Parse( Request.Form["Count"] );
for( int i = 0; i < nCount; i++ )
{
objRow = new TableRow();
objCell = new TableCell();
objCell.Text = Request["Path"].Split(',')[i];
objRow.Cells.Add( objCell );
objCell = new TableCell();
objCell.Text = Request["Size"].Split(',')[i];
objRow.Cells.Add( objCell );
objCell = new TableCell();
objCell.Text = Request["ContentType"].Split(',')[i];
objRow.Cells.Add( objCell );
objTable.Rows.Add( objRow );
}
}
</script>
|
Click the links below to run this code sample:
http://localhost/xupload/02_redirect_ie8.asp
http://localhost/xupload/02_redirect_ie8.aspx