download SharpUpload.cs
Language: C#
Copyright: (C) 2006 By MR Treebhoph Thoomsan, treebhoph@hotmail.com
LOC: 155
Project Info
WebLibraries For ASP.NET - SharpUpload(weblibraries)
Server: Spider_20090121_inc
Type: filesystem
...harpUpload1‑0‑Net2.zip\src\
   AssemblyInfo.cs
   EnumResult.cs
   FileUploaded.cs
   FileUploadedCollection.cs
   SharpUpload.cs
   SharpUploadConfig.cs
   SharpUploadEvent.cs

// Copyright (C) 2006  By MR Treebhoph Thoomsan, treebhoph@hotmail.com
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

using System;
using System.Collections;
using System.Collections.Specialized;
using System.IO;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Text;

namespace WebLib.Util
{
	public class SharpUpload
	{
		private const float KILO_BYTE_UNIT = 1027.977F;
		private bool isOverwrite;
		private bool autoCreateDir;
		private float maxSize;
		private StringCollection allowedExtensions;
		private String destinationDir;
		public  SharpUploadEventHandler OnCompletedUpload;
		
		private FileUploadedCollection filesUploaded;
		private HttpFileCollection postedFiles;

		public bool IsOverwriteFile
		{
			get{ return this.isOverwrite; }
			set{ this.isOverwrite = value; }
		}

		public bool IsAutoCreatedDir
		{
			get{ return this.autoCreateDir; }
			set{ this.autoCreateDir = value; }
		}

		public float MaxSize
		{
			get{ return this.maxSize; }
			set{ this.maxSize = value; }
		}

		public StringCollection AllowedExtensions
		{
			get{ return this.allowedExtensions; }
		}

		public String DestinationDir
		{
			get{ return this.destinationDir; }
			set
			{ 
				this.destinationDir = String.Concat( value.TrimEnd( '/' ), "/");
			}
		}

		
		public SharpUpload( )
		{
			this.filesUploaded	= new FileUploadedCollection( );
			this.allowedExtensions = new StringCollection( );
		}
		
		public void LoadConfig( String xmlConfigFile )
		{
			SharpUploadConfig config = new SharpUploadConfig( );
			config.Uploader = this;
			config.Load( xmlConfigFile );
		}

		public void Upload( )
		{
			this.Upload( false );
		}
		
		public void Upload( bool isAutoSaveFiles )
		{
			this.DoUpload( );

			if( this.OnCompletedUpload != null )
			{
				SharpUploadEventArgs eventArgs = new SharpUploadEventArgs( this.filesUploaded );
				this.OnCompletedUpload( this, eventArgs );
			}
			
			if( isAutoSaveFiles )
				this.SaveFiles();
		}

		public void SaveFiles( )
		{
			if( this.CreateDir( this.destinationDir ) )
			{
				foreach( FileUploaded fileUploaded in this.filesUploaded )
				{
					if( fileUploaded.UploadedResult == Result.Success  )
						fileUploaded.PostedFile.SaveAs( String.Concat( HttpContext.Current.Server.MapPath( this.DestinationDir ), fileUploaded.FileName ) );
				}
			}
			else
			{
				throw new System.IO.FileNotFoundException( "Can not save files because not found dir "+ HttpContext.Current.Server.MapPath( this.DestinationDir ) + ". You can solve this ploblem by setting \"SharpUpload.IsAutoCreateDir = true\"." );
			}
		}

		private void DoUpload( )
		{
			this.postedFiles = HttpContext.Current.Request.Files;

			for (  int i = 0; i <  this.postedFiles.Count; i ++ )
			{
				HttpPostedFile postedFile  = this.postedFiles[ i ];

				if( postedFile != null && postedFile.ContentLength > 0 )
				{
					FileUploaded fileUploaded  	= this.GetFileUploaded( postedFile );
					fileUploaded.SetSequence 	= i + 1;
			
					this.filesUploaded.Add( fileUploaded );
				}
			}
		}

		private FileUploaded GetFileUploaded( HttpPostedFile  postedFile )
		{
			FileUploaded file	= new FileUploaded( );
			file.SetFileName	= Path.GetFileName( postedFile.FileName );
			file.SetFileExtension	= Path.GetExtension( postedFile.FileName ).Substring( 1 ).ToLower();
			file.SetMimeType	= postedFile.ContentType;
			file.SetSize	= postedFile.ContentLength;   
			file.SetDataByte	= new byte[ file.FileSize ];
			file.PostedFile 	= postedFile;
			file.SetResult 	=  Result.Success;

			if ( ! this.isOverwrite )
			{
				file.SetFileName = this.GetUniqueName( HttpContext.Current.Server.MapPath( this.destinationDir )  + "/" + file.FileName);
			}
						
			postedFile.InputStream.Read( file.DataByte, 0, file.FileSize );

			if( ! this.IsValidExtensions( file.FileExtension ) )
				file.SetResult =  Result.MissExtension;

			if( ( this.maxSize * KILO_BYTE_UNIT < file.FileSize || file.FileSize < 1 ) && file.UploadedResult == Result.MissExtension )
				file.SetResult  =  Result.MissExtensionAndSize;
			else if( ( this.maxSize * KILO_BYTE_UNIT < file.FileSize || file.FileSize < 1 ) )
				file.SetResult =  Result.MissSize;

			return file;
		}

		private bool IsValidExtensions( String fileExtension )
		{		
			return this.allowedExtensions.Contains( fileExtension ) || this.allowedExtensions.Contains( "." +fileExtension );
		}

		private string GetUniqueName( String fileName)
		{
			string temp = fileName;

			for (int x=1; System.IO.File.Exists(temp); x++)
				temp = fileName.Substring(0, fileName.LastIndexOf(".")) +"_"+ x.ToString() + fileName.Substring(fileName.LastIndexOf("."));
			
			return new System.IO.FileInfo(temp).Name;
		}

		private bool CreateDir( String path )
		{					
			DirectoryInfo dir= new DirectoryInfo( HttpContext.Current.Server.MapPath ( path ) );
			
			if ( ! dir.Exists)
			{	
				if ( this.autoCreateDir )
				{
					dir.Create();
					return true;
				}
				else
					return false;
			}

			return true;		
		}
		
	}
}

About Koders | Resources | Downloads | Support | Black Duck | Submit Project | Terms of Service | DMCA | Privacy Policy | Site Map| Contact Us