Wednesday, February 4, 2009

Image Uploading

if (fluAddImage.FileName != "")
{
FileInfo Finfo = new FileInfo(fluAddImage.FileName);
string Ext = Finfo.Extension.ToLower();
if (Ext == ".jpg" || Ext == ".jpeg" || Ext == ".gif" || Ext == ".bmp" || Ext == ".png")
{

string rootfolder = GetPhysicalPath(Application["HomePath"] + Global.MemberPhotoPath);//path e.g http://localhost:4578/project/Imagesg

if (!Directory.Exists(rootfolder))
{
Directory.CreateDirectory(rootfolder);
}
DirectoryInfo dirInfo = new DirectoryInfo(rootfolder);
string PhotoName ="xyz" + Ext;
string Filename = rootfolder + PhotoName;
fluAddImage.SaveAs(Filename);
}
}


public static string GetPhysicalPath(string virtualPath)
{
string pageUrl = HttpContext.Current.Request.Url.PathAndQuery.ToString();
string filename = "";
filename = System.IO.Path.GetFileName(virtualPath);
string filepath = "";
if (virtualPath.IndexOf("localhost:") >= 0)
{
string path = virtualPath.Replace(HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Host, "");
if (path.IndexOf(":") >= 0)
{
int lenindex = path.IndexOf('/');
path = path.Remove(0, lenindex);
}
filepath = HttpContext.Current.Server.MapPath(path.Replace(HttpContext.Current.Request.Url.Scheme + "://www." + HttpContext.Current.Request.Url.Host, ""));
}
else
{
string path = "";
path = virtualPath.Replace(HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Host, "");
filepath = HttpContext.Current.Server.MapPath(path.Replace(HttpContext.Current.Request.Url.Scheme + "://www." + HttpContext.Current.Request.Url.Host, ""));
}
return filepath;
}