| Shreeman's profileShreemanBlogLists | Help |
|
June 23 FILE Upload Control in Asp.net 2.0[For Beginner]Another Old Post:-Saved in draft:-
Although Its a very common Utility and many might already have used it quite often.Till I am posting this post based on the Request I got thru email from member of our .net group.
His Requirement was how to upload a File from Client to Server and how to restrict the user to upload files graeter tehn 2 MB.Here I ll demonstarte a very small beginner routine to achieve the same:-
Here we ll use the new Asp.Net 2.0 FileUpload Control:-
Here is the Html part:-
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> < html >< head runat="server"><title>Untitled Page</title> </head> <body> <form id="form1" runat="server"> <div> <asp:FileUpload ID="FileUpload1" runat="server" Style="z-index: 100; left: 178px; position: absolute; top: 62px" BackColor="SkyBlue" BorderStyle="Double" Font-Bold=true/> <asp:Button ID="Upload" runat="server" OnClick="Button1_Click" Style="z-index: 101; left: 180px; position: absolute; top: 98px" Text="Upload To Server" BackColor="SteelBlue" BorderStyle="Outset" Font-Bold="True" ForeColor="White" Width="132px" /> <asp:Label ID="Label1" runat="server" Style="z-index: 103; left: 182px; position: absolute; top: 131px" Text="Label" Width="510px" BackColor="AliceBlue" BorderStyle="Solid" Font-Bold="True" Font-Names="Book Antiqua" Visible="False"></asp:Label> </div> </form> </body> </html>
and here is the CodeBehind:-
public partial class _Default : System.Web.UI.Page{ protected void Page_Load(object sender, EventArgs e) { FileUpload1.Focus(); } protected void Button1_Click(object sender, EventArgs e) { Label1.ForeColor = System.Drawing.Color.Magenta; if (FileUpload1.HasFile) try { if (FileUpload1.PostedFile.ContentLength - 1024*1024> 0) { Label1.Text = "Cann't upload File More then 1 Mb"; Exception ex = new Exception("FileBigger Then 1 Mb Not allowed"); throw (ex); } FileUpload1.SaveAs("C:\\UploadedFiles\\" + FileUpload1.FileName); Label1.Text = "File name: " + FileUpload1.PostedFile.FileName + "<br>" + FileUpload1.PostedFile.ContentLength/1024 + " Kb<br>" + "Content type: " + FileUpload1.PostedFile.ContentType; Label1.Visible = true; } catch (Exception ex) { Label1.Text = "ERROR: " + ex.Message.ToString(); Label1.Visible = true; } else { Label1.Text = "You have not specified a valid file."; Label1.Visible = true; } } } WHat Do You Want to See in Enterprise Library for .NET Framework 3.0Tom HAs posted a POst asking for Your View on What do you Like to see in EntripriSelibrary for .NEt 3.0.For All you feedback Post there .Here is the Link:-
June 21 VSTO- Version 3.0 JUNE CTP RELEASEDMicrosoft Pre-Release Software Visual Studio Tools For Office "V3" - June Community Technology Preview (CTP)The June 2006 Community Technology Preview of Microsoft Visual Studio Tools for Office "V3" is now available.
Note: This CTP requires you to have the beta 2 release of the 2007 Microsoft Office system. Please click here for more information and download instructions June 20 .NET 3.0 Is Coming but LINQ will not be a Part of itIt has been Announced in TECHED2006 that .NET 3.0 is Coming .Now you might want to know what all will be the part of .NET FrameWork 3.0.
Actually .NET 3.0 = WCF + WPF + WF + WCS+.NET Framework 2.0
IF you are thinking that LINQ,DLINQ will be part of it ..remember it is not and that will be part of some future version.The CLR and the BCL will be the same 2.0.50727 June 16 Programmatically getting the NameSpaces and Generic Factory to Load the ControlThis particular Post is more then a year older and today while looking for my Old post i found that it was saved but not published.Thus Publishing it:-
If you are into .Net Programming quite Often you ll get a scenario where by you need to invoke a control or a Class or a method of a particular class type in Runtime .That is During the design or compiletime you are not aware which controls or methods needed to be executed.Further you might also be facing a scenariowhere if you need to Generate same or Different Set of class or Assmebly (EXE or dll doesn't matter)dynamically .
Normally from a developer's stand point you ll Execute the same using Reflection.Rememebr Reflection gives you the black magic to even alter or execute a Private member too.Further Codegen feature of relflection.emit or codedom can help you in dynamically craeting an assembly or executing or invoking a type in the dynamic or static assembly.Although be aware of the CAS level security while dealing with a dynamic generated assembly particularly under what trust that assembly will run and what would be the trust level you can provide and finally the consequences of using in memory dlls.
Howvere this post is not about dynamic generation too [may be some other time].Here by i ll simply show the generic factory pattern you can use to invoke a assembly[dll or exe doesn't matter again].
I am aware of the fact that you might aware of the factory pattern and bit of CodeDom And reflection.However this post is mere a refresher for a new approach and an answer to a common but well hidden question.
My friend asked me while he was trying with generic factory that how he can get the namespace name...QUITE straight fwd right but believe me it took me 5 minutes and just when i was looking for seraching the reflection and reflector etc;;got it:-
Here is the Lione to get the Namespace:-
string nmspace=Assembly.GetExecutingAssembly().EntryPoint.DeclaringType.Namespace;
Coming to the FActory method .This is How you can use it to Laod dynamic Controls into ur UI:-
using System;
using System.Reflection; using System.Security; namespace WindowsApplication2 { /// <summary> /// Summary description for GenericFactory. /// </summary> public class GenericFactory { public GenericFactory() { // // TODO: Add constructor logic here // } public static object CreateObj( string asmName, string typeName, object[] constArgs )
{ // Instance to hold assembly Assembly asm = null; // Instance to Type Information Type typeInfo = null; // Load the Assembly.
try { // current executing assembly, which should be ur assembly // If the name of the assembly is not given then take the current executing assembly.
if ( ( asmName != "" ) || ( asmName != string.Empty ) ) // Load the assembly asm = Assembly.LoadFrom( asmName ); else asm = Assembly.GetExecutingAssembly(); } catch( SecurityException e ) { // To do: Exception Handling throw e; } catch( Exception e ) { // To do: Exception Handling Mechs throw e; } // Load type information
try { // use type name to get type from asm; note we WANT case specificity typeInfo = asm.GetType( typeName.Trim(), true, false ); if ( constArgs != null ) // Create the object dynamically with args return Activator.CreateInstance( typeInfo, constArgs ); else // Create the object dynamically. return Activator.CreateInstance( typeInfo ); } catch( Exception e ) { // To do: Exception Handling Mechs throw e; } } } } private void Form2_Load(object sender, System.EventArgs e)
{ object[] objarr={"shreeman"}; object obj=GenericFactory.CreateObj("","WindowsApplication2.UserControl1",objarr); this.panel1.Controls.Add((UserControl)obj); } private UserControl getnextcontrol(string classname) { string nmspace=Assembly.GetExecutingAssembly().EntryPoint.DeclaringType.Namespace; string typename=nmspace+"."+classname; object[] objarr={"shreeman"}; object obj=GenericFactory.CreateObj("",typename,objarr); return((UserControl)obj); } string getnextcontrolname(string currctrlname) { switch ( currctrlname) { //this is for demo purposes only and you need to change ur code accordingly
case "UserControl1" :
return "UserControl2"; case "UserControl2" :
return "UserControl3"; case "UserControl3" :
return "UserControl1"; default :
return null; } } private void button1_Click(object sender, System.EventArgs e) { UserControl uc=new UserControl (); uc=(UserControl)this.panel1.Controls[0]; uc=getnextcontrol( getnextcontrolname(this.panel1.Controls[0].Name)); this.panel1.Controls.Add(uc); this.panel1.Controls.SetChildIndex(uc,0); }
//What if i want to Run an exe using Reflection.Remembervthat .Net unit is Assembly be it dll or Exe reflection doesn't mind
// object obj=GenericFactory.CreateObj("WindowsApplication2.exe","WindowsApplication2.Form1",null); // //this.panel1.Controls.Add((UserControl)obj); // ((Form)obj).Show(); Hope that I shade few more lights into the older topic. June 07 LDAP verificationOften this is a common requirement to validated the user from the active directory or LDAP.Here by i am showing you a simple routine which will take the user alias for the domain and retrieve the userinfo:- Fisrt of all you need to import the directory class:- using System.DirectoryServices;
Keep this Routine in a Class let say DirectoryServiceHelper:-
public static void GetUserInfo(string alias,out string fullname,out string firstname,out string lastname) {
string delimStr = @"\"; char [] delimiter = delimStr.ToCharArray(); string[] domain;
//To get the domain name try { domain=alias.Split(delimiter,2); string LookUpDirectory ="LDAP://" +domain[0];
DirectoryEntry entry = new DirectoryEntry(LookUpDirectory); DirectorySearcher dSearch = new DirectorySearcher(entry);
//Filter by alias name dSearch.Filter = "(mailnickname= " + domain[1] + ")"; fullname = ""; firstname = ""; lastname = "";
foreach(SearchResult sResultSet in dSearch.FindAll()) {
if(sResultSet.Properties.Contains("cn")) { //assign fullname fullname = sResultSet.Properties["cn"][0].ToString() ;
}
if(sResultSet.Properties.Contains("givenName")) { //assign firstname firstname = sResultSet.Properties["givenName"][0].ToString() ;
}
if(sResultSet.Properties.Contains("sn")) { //Assign lastname lastname = sResultSet.Properties["sn"][0].ToString() ;
}
}
} catch ( Exception ex ) { throw ex; } }
and in ur Main Routne or Startup Routine validet the user:-
public void AuthenticateUser() {
string alias; string firstName; string lastName; string name; WindowsIdentity windowIdentity = WindowsIdentity.GetCurrent(); alias = windowIdentity.Name; try { DirectorySvcHelper userAuthetnicate = new DirectorySvcHelper(); DirectorySvcHelper.GetUserInfo( alias, out name, out firstName, out lastName ); } catch( Exception ex ) { throw ex; } |
|
|