November 28, 2011

Coding in server side for uploading Photo


protected void imgbtnAddPhoto_Click(object sender, ImageClickEventArgs e)

{

try

{

if (flpAddPhoto.PostedFile != null && flpAddPhoto.HasFile)

{





System.IO.FileInfo inf = new FileInfo(flpAddPhoto.FileName);

string Extension = inf.Extension.ToString().ToLower();

if (Extension == ".jpg" || Extension == ".jpeg" || Extension == ".png" || Extension == ".bmp" || Extension == ".gif")

{

double length = (flpAddPhoto.PostedFile.InputStream.Length) / 1024;

if (length <= 51200)

{

string fileName = System.IO.Path.GetFileName(flpAddPhoto.PostedFile.FileName);

if (fileName != "")

{

SPSecurity.RunWithElevatedPrivileges(delegate()

{

string _fileTime = DateTime.Now.ToFileTime().ToString();

string _fileorgPath = System.IO.Path.GetFullPath

(flpAddPhoto.PostedFile.FileName);

string _newfilePath = _fileTime + "~" + fileName;

string tempFolder = Environment.GetEnvironmentVariable("TEMP");

string _filepath = tempFolder + _newfilePath;

flpAddPhoto.PostedFile.SaveAs(_filepath);

Stream fStream = flpAddPhoto.PostedFile.InputStream;

byte[] Contents = new byte[fStream.Length];

fStream.Read(Contents, 0, (int)fStream.Length);

imgPhoto.ImageUrl = _filepath;

if (insert(fileName, _filepath))

{

}

ViewState["PhotoContent"] = Contents;

ViewState["FilePath"] = _filepath;

ViewState["Filename"] = fileName;

fStream.Close();

fStream.Dispose();

});

}

}

}

}

}

catch (Exception ex)

{

ErrorLog.Add(Convert.ToString(ex.Message), Convert.ToString(ex.StackTrace));

Response.Redirect("/_catatlogs/masterpage/Error.aspx");

}

}

No comments:

Post a Comment