I found a good code in the forum by JohnWolf [
http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?catid=288&threadid= 1183607&CFID=1428577&CFTOKEN=238c2ffe0c6da910-023C7083-B9BF-9584-5A21B29951DE176
B&jsessionid=9630acc164a23a102f6a ] and slightly modified it to fit in a page
instead of a WebService. Here is the code, and it works: (Note: if you put a
breakpoint inside the Page_Load event handler, you will see it is hit two times
as happened to me, first time with no file uploaded, and second time with the
uploaded file, though I don't know the reason)
public class handleFileUpload : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
try
{
//HTTP Context to get access to the submitted data
HttpContext postedContext = HttpContext.Current;
//File Collection that was submitted with posted data
HttpFileCollection files = postedContext.Request.Files;
//Make sure a file was posted
string fileName =
(string)postedContext.Request.Form["fileName"];
if (files.Count == 1 && files[0].ContentLength > 1 && fileName
!= null && fileName != "")
{
//The byte array we'll use to write the file with
byte[] binaryWriteArray = new
byte[files[0].InputStream.Length];
//Read in the file from the InputStream
files[0].InputStream.Read(binaryWriteArray, 0,
(int)files[0].InputStream.Length);
//Open the file stream
FileStream objfilestream = new
FileStream(MapPath("files")+"\\" + fileName, FileMode.Create,
FileAccess.ReadWrite);
//Write the file and close it
objfilestream.Write(binaryWriteArray, 0,
binaryWriteArray.Length);
objfilestream.Close();
}
}
catch (Exception ex)
{
throw new Exception("Problem uploading file: " + ex.Message);
}
}
}