|
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO; //Directory, Path, FileInfo
public partial class _Default : System.Web.UI.Page
{
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 (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);
}
}
//Clear the list, otherwise the ImageList has items
//Which the MuliUpload component does not have
ImageList.ImageListItemCollection.Clear();
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.