File upload question

Hi experts,

My coding is attached.

I don't know why when I press the Upload button, it sometimes run the code correctly, but sometimes it just display "The page cannot be displayed". I tried to run the debugger and found that sometimes the "private void btnUpload_Click" even didn't fire.

I have no idea on how to solve this problem. Can anyone give me a hand??
using System;
using System.IO;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;
using VE.Classes;
 
namespace VE
{
	/// <summary>
	/// Summary description for frmUpload.
	/// </summary>
	public class frmUpload : System.Web.UI.Page
	{
		protected System.Web.UI.WebControls.Label lblMsg;
		protected System.Web.UI.HtmlControls.HtmlInputFile ffUpload;
		protected System.Web.UI.WebControls.Label lblFileSize;
 
		int fileMaxSize = int.Parse(ConfigurationSettings.AppSettings["maxUploadFileSize"]) * 1024;
		protected System.Web.UI.WebControls.Button btnUpload; // Control in Web.config
		int fileMinSize = 1;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			// Put user code to initialize the page here
		}
 
		#region Web Form Designer generated code
		override protected void OnInit(EventArgs e)
		{
			//
			// CODEGEN: This call is required by the ASP.NET Web Form Designer.
			//
			InitializeComponent();
			base.OnInit(e);
		}
		
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{    
			this.btnUpload.Click += new System.EventHandler(this.btnUpload_Click);
			this.Load += new System.EventHandler(this.Page_Load);
 
		}
		#endregion
 
		private void btnUpload_Click(object sender, System.EventArgs e)
		{
			string Path = ConfigurationSettings.AppSettings["tmpFilePath"];
 
			if (ffUpload.PostedFile.FileName != "")
			{
				lblMsg.Text = "The file is uploading.";
				uploadFile(Path, ffUpload);
				lblMsg.Text = "The file has been uploaded.";
				btnUpload.Enabled = false;
			}
		}
 
		public int uploadFile(string Path, System.Web.UI.HtmlControls.HtmlInputFile File)
		{
			// 0 - error, 1 - success, 2 - invalid file size, 3 - invalid file type
			if (File.PostedFile.FileName != "")
			{
				if (!(System.IO.Directory.Exists(Path)))
					return 0;
 
				if (!(validateFileSize(File.PostedFile.ContentLength)))
					return 2;
 
				if (!(validateFileType(File.PostedFile.FileName)))
					return 3;
 
				string tmpPath;
				tmpPath = Path + System.IO.Path.GetFileName(File.PostedFile.FileName);
				File.PostedFile.SaveAs(tmpPath);
				return 1;
			}
			return 0;
		}
 
		public void deleteFile(string Path, string FileName)
		{
			if (System.IO.File.Exists(Path + FileName))
				System.IO.File.Delete(Path + FileName);
		}
 
		private bool validateFileSize(int fileSize)
		{
			if (fileSize < fileMinSize || fileSize > fileMaxSize)
				return false;
			else
				return true;
		}
 
		private bool validateFileType(string fileName)
		{
			if (fileName.Substring(fileName.Length - 4, 4).ToLower() == ".pdf")
				return true;
			else
				return false;
		}
	}
}

Open in new window

kennysflauAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

codeclayCommented:
I dont see why the event gets fired some times and not other times . But some observation on reading ur code


private void btnUpload_Click(object sender, System.EventArgs e)
            {
                  string Path = ConfigurationSettings.AppSettings["tmpFilePath"];
 
                  if (ffUpload.PostedFile.FileName != "")
                  {
                        lblMsg.Text = "The file is uploading.";  ///This will never get displayed as it will be overriten by  "The file has been uploaded"
                        uploadFile(Path, ffUpload); ///Function returns an int value as status , but you are never using it .
                        lblMsg.Text = "The file has been uploaded.";
                        btnUpload.Enabled = false;   ///This will be disabled after postback  and you wont be able to uplaod another file .
                  }
            }
0
codeclayCommented:
Could you please post the aspx code also so that we could do some analysis of your problem ?
0
kennysflauAuthor Commented:
Yes here it is, thanks!
<%@ Page language="c#" Codebehind="frmUpload.aspx.cs" AutoEventWireup="false" Inherits="VE.frmUpload" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
	<HEAD>
		<title>frmItemInfo</title>
		<meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
		<meta content="C#" name="CODE_LANGUAGE">
		<meta content="JavaScript" name="vs_defaultClientScript">
		<meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
		<meta http-equiv="pragma" content="no-cache">
		<meta http-equiv="Expires" content="-1">
		<LINK href="StyleSheet.css" type="text/css" rel="stylesheet">
		<script src="Include/JavaInc/JScript.js"></script>
	</HEAD>
	<body leftMargin="0" topMargin="0">
		<form id="Form1" method="post" runat="server">
			<table height="100%" cellSpacing="0" cellPadding="0" width="100%" border="0">
				<tr bgColor="#333333">
					<td width="20" height="20">&nbsp;</td>
					<td class="heading3" bgColor="#333333" colSpan="2" height="30"></td>
					<td width="20" height="20">&nbsp;</td>
				</tr>
				<tr>
					<td class="NCCommonBold" colSpan="4">&nbsp;<asp:label id="lblMsg" Runat="server"></asp:label></td>
				</tr>
				<tr>
					<td class="NCCommonBold" colSpan="4">&nbsp;<INPUT id="ffUpload" type="file" size="50" runat="server">&nbsp;
							<asp:label id="lblFileSize" runat="server"></asp:label></td>
				</tr>
				<tr>
					<td colSpan="4">&nbsp;
						<asp:Button id="btnUpload" runat="server" Text="Upload"></asp:Button></td>
				</tr>
				<tr>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
				</tr>
				<tr bgColor="#333333">
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
					<td>&nbsp;</td>
				</tr>
			</table>
		</form>
	</body>
</HTML>

Open in new window

0
kennysflauAuthor Commented:
I just found that the reason is because I need to increase the upload file size limit in web.config... say set it to 50MB, as attached
<httpRuntime maxRequestLength="50000" useFullyQualifiedRedirectUrl="true" executionTimeout="45"/>

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.