Thursday, April 10, 2008

Code for upload an image

Code in cs side.
First copy paste in note pad and then change [ to <>

private string SaveLocation = string.Empty; private string imgPath; public string ImagePath { get { return imgPath; } set { imgPath = value; } } protected void Page_Load(object sender, EventArgs e) { UpLoadImage();
} public void UpLoadImage() { if ((File1.PostedFile != null) && (File1.PostedFile.ContentLength > 0)) { string fn = System.IO.Path.GetFileName(File1.PostedFile.FileName); SaveLocation = Server.MapPath("UserImages") + @"\" + DateTime.Now.ToFileTimeUtc().ToString()+fn; string FileExtension = SaveLocation.Substring(SaveLocation.LastIndexOf('.')); if ((FileExtension.ToLower() == ".jpg") (FileExtension.ToLower() == ".bmp") (FileExtension.ToLower() == ".gif")) { try { File1.PostedFile.SaveAs(SaveLocation); imgPath = SaveLocation; // return SaveLocation;
lblmsg.Text = Utilitty.Messages(3); } catch { lblmsg.Text="Error:.... "; // return ""; } } else { lblmsg.Text = "Please choose bmp/jpg/gif images."; // return ""; } } else { lblmsg.Text="Please select a file to upload."; // return ""; }
}


In html side
[script language="javascript" type="text/javascript"]/*function setImage(string) {debugger ;
var index = string.lastIndexOf("."); var Extension=string.substring(index+1,(string.length)); Extension=Extension.toUpperCase(); if(Extension =="BMP" Extension=="JPEG" Extension == "GIF" Extension == "JPG") { } else { document.getElementById("FileUpload1_File1").value=' '; alert('Please select bmp/jpg/gif image'); return false; } }*/
[/script]
[table][tr][td style="width: 476px"] [input type="file" id="File1" name="File1" runat="server" style="width: 223px" /]
[/td]
[/tr][tr][td style="width: 476px"][asp:Label ID="lblmsg" runat="server"][/asp:Label][/td][/tr]
[/table]

1 comment:

Anonymous said...

firstly i would like to wish u about done a great time on this blog,,,
but here u pasted some code it's fine but it seems like a story not a code,, lol i think no one put patience on it to know what is in it.