protected void btnUpload_Photo_Click(object Sender, EventArgs e) { // Read the file and save it if (FileUpload_Photo.HasFile) { string filePath = FileUpload_Photo.PostedFile.FileName; string filename = Path.GetFileName(filePath); string ext = Path.GetExtension(filename); // Get the size in bytes of the file to upload. int fileSize = FileUpload_Photo.PostedFile.ContentLength; int limit = 102400; if (ext == ".jpg" || ext == ".jpeg" || ext == ".png" || ext == ".gif") { if (fileSize < limit) { string fileName = "Photo" + ext; FileUpload_Photo_Loc.Text = filename; string destPath = "C:/Users/Kiran/Documents/GitHub/Recruitment-Portal/server_photos/" + fileName; photoPreview.ImageUrl = "~/server_photos/" + fileName; FileUpload_Photo.SaveAs(destPath); //this will save the file in the application directory //to view the file when you click hyperlink below: HyperLinkPhoto.NavigateUrl = "file:///" + destPath; lblMessage_Photo_Loc.Text = fileName; lblMessage_Photo.ForeColor = System.Drawing.Color.Green; lblMessage_Photo.Text = "Photo Uploaded Successfully"; } else { lblMessage_Photo.ForeColor = System.Drawing.Color.Red; lblMessage_Photo.Text = "File Size exceeded " + limit / 1024 + " KB"; } } else { lblMessage_Photo.ForeColor = System.Drawing.Color.Red; lblMessage_Photo.Text = "File format not recognised." + " Upload jpg/png/gif formats"; } } else { lblMessage_Photo.ForeColor = System.Drawing.Color.Red; lblMessage_Photo.Text = "File format not recognised." + " Upload jpg/png/gif formats"; } }
protected void btnPreview_Click(object sender, EventArgs e) { if (FileUpload_Photo.HasFile) { string fileName = FileUpload_Photo.FileName.ToUpper(); string fl = Server.MapPath("/Images/" + lblNamePhoto.Text.ToUpper()); if (File.Exists(fl)) { File.Delete(Server.MapPath("/Images/" + lblNamePhoto.Text.ToUpper())); } FileUpload_Photo.SaveAs(Server.MapPath("/Images/" + fileName)); imgPhotoEmployee.ImageUrl = "~/Images/" + fileName; lblNamePhoto.Text = fileName; } }