Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
using System;
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.IO;

namespace FileUploader
{
	/// <summary>
	
	/// This product is free for all. don't economic use.
	/// </summary>
	public class frmDefault : System.Web.UI.Page
	{
		public string filesurl="files/";
		//public string filesurl="http://www.files.somee.com/files/";
		protected System.Web.UI.WebControls.Label lblFileUrlHint;
		protected System.Web.UI.WebControls.Button btnUpload;
		protected System.Web.UI.HtmlControls.HtmlInputFile htmInput;
		protected System.Web.UI.WebControls.Label lblErrorUploading;
		protected System.Web.UI.WebControls.Label lblErrorFileExists;
		protected System.Web.UI.WebControls.Label lblErrorFileExt;
		protected System.Web.UI.WebControls.HyperLink lnkFileUrl;
		protected System.Web.UI.WebControls.HyperLink lnkUploaded;
		protected System.Web.UI.WebControls.Label lblLargeFile;
		protected System.Web.UI.WebControls.Label lblUploaded;
	
		private void Page_Load(object sender, System.EventArgs e)
		{
			//filesurl=Request.Url.LocalPath+"/files/";
			htmInput.Accept="image/*";
			//this sh*ts, need to be shut up.
			lblErrorUploading.Visible=false;
			lblErrorFileExists.Visible=false;
			lnkFileUrl.Visible=false;
			lblFileUrlHint.Visible=false;
			lblErrorFileExt.Visible=false;
			lblUploaded.Visible=false;
			lblLargeFile.Visible=false;
			// 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 SaveFile(string strPath, ref byte[] Buffer) 
		{ 
			//save file to server
			FileStream myFile = new FileStream(strPath, FileMode.Create); 

			myFile.Write(Buffer, 0, Buffer.Length); 

			myFile.Close(); 
		}
		private bool LocalFilePath(string file,out string fpath,out string url)
		{
			//generate local file path
			string fname=System.IO.Path.GetFileName(file);
			string local=Server.MapPath("files\\");
			string ext=System.IO.Path.GetExtension(file);
			string result=local+fname;
			if (System.IO.File.Exists(result))
			{
				fname=System.IO.Path.GetFileNameWithoutExtension(file);
				for (int i=0;i<50;i++)
				{
					result=local+fname+i.ToString()+ext;
					if (!System.IO.File.Exists(result))
					{fname=fname+i.ToString()+ext;
						break;
					}
				}
				url=filesurl+fname;
				fpath=result;
				if (System.IO.File.Exists(result))
					return false;
				else
					return true;
			}
			else
			{
				url=filesurl+fname;
				fpath=result;
				return true;
			}
		}

		private void btnUpload_Click(object sender, System.EventArgs e)
		{
            const string accept = "*.png;*.jpg;*.gif;*.zip;*.rar;*.Docx;*.xlsx";
			if ( htmInput.PostedFile != null ) 
			{ 
				string local,url,ext;
				HttpPostedFile myFile = htmInput.PostedFile;
				//test file laws
				ext=Path.GetExtension(myFile.FileName);
				if (myFile.ContentLength>(4*(1024*1024)))
				{
					lblLargeFile.Visible=true;
					return;
				}
				if (accept.IndexOf(ext.ToLower())==-1)
				{
					lblErrorFileExt.Visible=true;
					return;
				}
				if (myFile.ContentLength==0)
				{
					lblErrorUploading.Visible=true;
					return;
				}
				//recive files length
				int nFileLen = myFile.ContentLength;
				byte[] myData = new byte[nFileLen]; 
				myFile.InputStream.Read(myData, 0, nFileLen);
				//test file size law
				if (!LocalFilePath(myFile.FileName,out local,out url))
				{
					lblErrorFileExists.Visible=true;
					return;
				}
				//and save it
				SaveFile(local,ref myData);
				//now go and F**k it
				lnkFileUrl.Text=System.IO.Path.GetFileName(url);
				lnkFileUrl.NavigateUrl=url;
				lnkFileUrl.Visible=true;
				lblFileUrlHint.Visible=true;
				lblUploaded.Visible=true;
			} 
			else 
			{ 
				//Oh! No! sending error!
				lblErrorUploading.Visible=true;// Fail 
			} 

		}

	}
}
Posted
Comments
Manas Bhardwaj 15-Nov-14 7:05am    
and the problem is?
Dave Kreskowiak 15-Nov-14 9:33am    
Your "question" isn't a question and doesn't make any sense no matter who many time you read it.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900