file uploading

  • Thread starter Thread starter Philip Townsend
  • Start date Start date
P

Philip Townsend

I am having difficulties implementing a file uploader. Code is posted
below. The txtFile control contains nothing when submitted, but the
click event is definitely firing the appropriate method. Can any body
help?

protected System.Web.UI.HtmlControls.HtmlInputFile txtFile;
protected System.Web.UI.HtmlControls.HtmlInputButton cmdUpload;


public void uploadFile(object sender, EventArgs args)
{
if(txtFile.PostedFile!=null)
{
String strQFile=txtFile.PostedFile.FileName.ToString();
String strFile=strQFile.Substring(strQFile.LastIndexOf(@"\")+1);
String strFileType=txtFile.PostedFile.ContentType;
int fileSize=txtFile.PostedFile.ContentLength;

// validate the file length
if(fileSize<=0)
{
Response.Write(String.Format(
"<font color=\"red\">Uploading of {0} failed: File is zero
length</font>"));
}
// upload the file
else
{
txtFile.PostedFile.SaveAs(Server.MapPath(@"images/fader/" +
strFile));
Response.Write(String.Format(
"<font color=\"red\">File: {0} successfully uploaded!</font>"));
}
}
else
Response.Write("<font color=\"red\">File is null</font>");
}
private void Page_Load(object sender, System.EventArgs e)
{
txtFile.Accept="text/*";
}
 
Hi,

How big is the file you are uploading when you try it out?

By default the Machine.config only allows files of 4MB to be uploaded,
unless you've overridden it in your Web.config.

If this is the issue your having then your probably going to run into the
same problem as me. That is how to know if the postedfile object is null
because the user didn't upload a file or because the file exceeded the
size limit?

Regards,
Peter Row
 
Back
Top