|
|||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionThe HTML file input control and the ASP.NET BackgroundThis article refers to the open-source controls of “Memba Velodoc XP Edition”, which you can download from here (this page provides links to Codeplex, Google code, and Sourceforge.NET) and which are distributed under the GPL license. These controls include a Using the codeIn Visual Studio 2005, create a new ASP.NET AJAX-Enabled Web Site, and add a reference to Memba.WebControls.XP.dll which contains the
Open the Default.aspx page and add the <form id="form1" runat="server" enctype="multipart/form-data">
<asp:ScriptManager ID="ScriptManager" runat="server" />
<!-- MultiUpload2 -->
<mbui:MultiUpload2 ID="MultiUpload" runat="server"
Text="Click me"
CssClass="cssMultiUpload"
HoverCssClass="cssHoverMultiUpload">
</mbui:MultiUpload2>
<!-- MultiUpload2 -->
<hr />
<div id="divDisplay" style="min-height:50px;">
<asp:Literal ID="litDisplay" runat="server"></asp:Literal>
</div>
<asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click" />
</form>
The form also includes a You may have to register the tag prefix at the top of the page, using the following statement: <%@ Register Assembly="Memba.WebControls.XP"
Namespace="Memba.WebControls" TagPrefix="mbui" %>
For file uploads to work, you need to make two important settings:
<httpRuntime maxRequestLength="1000000" executionTimeout="20000"/>
From the standpoint of presentation, the <style type="text/css">
<!--
.cssMultiUpload{
color:CornflowerBlue;
text-decoration:none;
}
.cssHoverMultiUpload{
color:CornflowerBlue;
text-decoration:underline;
}
//-->
</style>
Your
Now that our page is designed, we need the code to save the posted files. Double click the Submit button ( private const string TARGET_DIR = "~/";
protected void btnSubmit_Click(object sender, EventArgs e)
{
//Check target directory
string sTargetDir = Request.MapPath(TARGET_DIR);
System.Diagnostics.Debug.Assert(Directory.Exists(sTargetDir));
StringBuilder objDisplay = new StringBuilder();
objDisplay.Append("<h3>You have just saved:</h3><ul>");
//Iterate through posted files (foreach does not work with empty file inputs)
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFile objFile = Request.Files[i];
//Make sure file input has content
if ((objFile.ContentLength > 0) && (objFile.FileName.Length > 0))
{
//Get target file path for save as
string sFileName = Path.GetFileName(objFile.FileName);
string sFilePath = Path.Combine(sTargetDir, sFileName);
FileInfo objFileInfo = new FileInfo(sFilePath);
//No business rule, i.e. we just want to avoid failure
if (objFileInfo.Exists)
{
objFileInfo.Attributes &= ~FileAttributes.ReadOnly;
objFileInfo.Delete();
}
//Save file
objFile.SaveAs(sFilePath);
//Append link
objDisplay.AppendFormat("<li><a href=\"{0}\">{1}</a></li>",
this.ResolveUrl(Path.Combine(TARGET_DIR, sFileName)), sFileName);
}
}
//Display in DIV
objDisplay.Append("</ul>");
litDisplay.Text = objDisplay.ToString();
}
Add the following statements at the top of Default.aspx.cs: using System.IO; //Directory, Path, FileInfo
using System.Text; //StringBuilder
Press F5 to run the page, click the We would now like to display the names of the files as they are selected. Add the following script just before the <script type="text/javascript">
<!--
// Declare global variables for the various controls
var g_MultiUpload;
var g_Display = "<h3>You have selected:</h3><ul>";
//pageLoad function of ASP.NET Ajax Extensions framework
function pageLoad()
{
//Get a reference to the MultiUpload control
g_MultiUpload = $find("<%= MultiUpload.ClientID %>");
//Add en event handler for the browse event
if(g_MultiUpload)
g_MultiUpload.add_browse(onBrowse);
}
//pageUnload function of ASP.NET Ajax Extensions framework
function pageUnload()
{
if(g_MultiUpload)
g_MultiUpload.remove_browse(onBrowse);
}
//Event handler for the browse (click) event of the MultiUpload control
function onBrowse(sender, args)
{
g_Display += "<li>" + args.get_value() + "</li>";
$get("divDisplay").innerHTML = g_Display + "</ul>";
}
//-->
</script>
ASP.NET AJAX Extensions provide two important JavaScript event handlers:
The script above implements an event handler for the Press F5 to run the project, and click the We would now want to interact with the list of selected files, especially to remove files, but this is the subject of a follow-up article entitled Professional file uploads with the MultiUpload and ImageList server controls. Points of interestThis History
|
||||||||||||||||||||||||||||||||||||||||