|
||||||||||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||||||||||
|
Announcements
Chapters
Services
Feature Zones
|
IntroductionImageList and MultiUpload have recently been described in two articles:
Our objective is to realize a professional GUI for uploading files, and this is achieved by combining BackgroundThis article refers to the open-source controls of the “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 an 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 both the <form id="form1" runat="server" enctype="multipart/form-data">
<asp:ScriptManager ID="ScriptManager" runat="server">
<Scripts>
<asp:ScriptReference Path="~/scripts/Memba.Utils.js" />
</Scripts>
</asp:ScriptManager>
<!-- MultiUpload2 -->
<mbui:MultiUpload2 ID="MultiUpload" runat="server"
Text="Choose file..."
Width="100px"
CssClass="cssMultiUpload"
HoverCssClass="cssMultiUploadHover">
</mbui:MultiUpload2>
<!-- MultiUpload2 -->
<!-- ImageList -->
<mbui:ImageList ID="ImageList" runat="server"
CssClass="cssList"
ItemCssClass="cssItem"
ItemHoverCssClass="cssItemHover"
ImageCssClass="cssImage"
TextCssClass="cssText"
RemoveCssClass="cssRemove"
RemoveTooltip="Remove from selection"
LinesOfText="2"
Height="92px"
Width="420px">
</mbui:ImageList>
<!-- ImageList -->
<input type="button" id="ClearButton" value="Clear" onclick="onClear();" />
<asp:Button ID="SubmitButton" runat="server" Text="Submit" OnClick="SubmitButton_Click" />
</form>
This form also includes an HTML button ( 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" %>
Make sure you add a script reference to Memba.Utils.js, which contains a necessary utility function to create GUIDs in JavaScript. For file uploads to work, you need to make two important settings:
The inline CSS styles used for the presentation of
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 SubmitButton_Click(object sender, EventArgs e)
{
//Check target directory
string sTargetDir = Request.MapPath(TARGET_DIR);
System.Diagnostics.Debug.Assert(Directory.Exists(sTargetDir));
//Iterate through posted files
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);
}
}
//Clear the list, otherwise the ImageList has items
//Which the MuliUpload component does not have
ImageList.ImageListItemCollection.Clear();
}
Add the following statement at the top of the file: using System.IO; //Directory, Path, FileInfo
The important thing to note here is that the browser clears HTML file input controls each time it loads the page, especially after a postback. There is no way to set the value of a file input control, otherwise web servers could obtain user files without their consent. There is no workaround to this security restriction. Our We could run the page at this stage and upload files, but we need some JavaScript client code to display, in the <script type="text/javascript">
<!--
// Declare global variables for the various controls
var g_MultiUpload;
var g_ImageList;
//pageLoad function of ASP.NET Ajax Extensions framework
function pageLoad()
{
//Get a reference to the MultiUpload control and
//add en event handler for the browse event
g_MultiUpload = $find("<%= MultiUpload.ClientID %>");
if(g_MultiUpload)
g_MultiUpload.add_browse(onBrowse);
//Get a reference to the ImageList control and
//add en event handler for the browse event
g_ImageList = $find("<%= ImageList.ClientID %>");
if(g_ImageList)
g_ImageList.add_remove(onRemove);
}
//pageLoad function of ASP.NET Ajax Extensions framework
function pageUnload()
{
if(g_MultiUpload)
g_MultiUpload.remove_browse(onBrowse);
if(g_ImageList)
g_ImageList.remove_remove(onRemove);
}
//Event handler for the browse (click) event of the MultiUpload control
function onBrowse(sender, args)
{
if((g_ImageList) && (g_MultiUpload))
{
//Search for the item in the Imagelist
if (g_ImageList.find_item(args.get_value()).length > 0)
{
alert("file already in list");
//The item already exists,
//we can remove the duplicate INPUT in the MultiUpload control
g_MultiUpload.removeInput(args.get_id());
}
else
{
//Since the item is not found in the ImageList,
//create a new item
var item = new Memba.WebControls.ImageListItem(
Memba.Utils.newGuid(),
'<%= this.ResolveClientUrl("~/images/upload.gif") %>',
args.get_value(),
args.get_value(),
args.get_id()
);
//Add the new item to the ImageList
g_ImageList.add_item(item);
}
//We can do some tracing which will display in the TraceConsole textarea
Sys.Debug.trace(g_ImageList.get_count()
+ " files in image list, and " + g_MultiUpload.get_count()
+ " files in MultiUpload control");
}
}
//Event handler for the remove event of the ImageList control
function onRemove(sender, args)
{
if((g_ImageList) && (g_MultiUpload))
{
//Upon clicking the remove icon in the ImageList,
//remove the corresponding INPUT in the MultiUpload control
g_MultiUpload.removeInput(args.get_tag());
//We can do some tracing which will display in the TraceConsole textarea
Sys.Debug.trace(g_ImageList.get_count()
+ " files in image list, and " + g_MultiUpload.get_count()
+ " files in MultiUpload control");
}
}
//Event handler for the click event of the clear button
function onClear()
{
if(g_MultiUpload)
g_MultiUpload.clear();
if(g_ImageList)
g_ImageList.clear();
}
//-->
</script>
ASP.NET AJAX extensions provide two important JavaScript event handlers:
The script above implements event handlers for the Press F5 to run the project, and click the At this stage, finalizing a professional upload page would require a progress indicator. We recommend reading Using AJAX to display the progress of an ASP.NET server task. If you want to combine all these controls together, I recommend downloading the source code of the Memba Velodoc XP Edition and the developer tutorial included, which will help you get to the next stage. Points of interestThis History
|
|||||||||||||||||||||||||||||||||||||||||||||||