Groups | Blog | Home
all groups > flash actionscript > april 2006 >

flash actionscript : Re: Flash 8 File Upload with ASP.NET


JK_Mtl
4/5/2006 11:02:51 PM
I have been trying to upload files using ASP.NET too, and it is driving my
crazy. I also get a Request.ContentLength equal to 0, no matter if I try it
locally or using my server. From the Flash side, I am using the standard code
that is available in the help files and pretty much anywhere else, so I am
figuring out that the problem is on the ASP.NET side. So if anyone can help me
out and let me know what code they are using with ASP.NET, that would be
great!! I am working with C# by the way. Thanx!!!
Deniom
9/26/2006 12:00:00 AM
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);
}
}
}

AddThis Social Bookmark Button