Hi
I just want to know how to display the Image Upload with currently image then remove image how will do this??
Thanks:)

Dani AI

Generated

Building on 's server-side save/display tip, the remaining pieces are safe deletion, keeping a single "primary" photo, and a quick client-side preview so the uploaded image appears immediately. The suggested approach below focuses on data design, access checks, and practical server/client snippets to handle delete and "set as primary" without repeating the save/display code already shown.

Keep a small Photos table (PhotoId, UserId, StoredFileName, IsPrimary, UploadedAt, MimeType, Size). On upload store a unique filename (GUID + extension) and a DB row. Enforce whitelist checks (mime/extension) and size limits. Store an absolute uploads root in configuration rather than building paths from untrusted input.

Server-side delete workflow (C#): verify the requestor owns the photo, build a full path from the configured uploads root plus the stored filename, ensure the resolved full path begins with the uploads root (prevent path traversal), delete the file if present, remove or flag the DB row, and—if the deleted photo was primary—atomically pick another photo (e.g., most recent) and set its IsPrimary flag.

// uploadsRoot should be a full absolute path read from config
string uploadsRoot = ConfigurationManager.AppSettings["UserPhotosPath"];
var photo = PhotoRepo.Get(photoId);
if (photo == null || photo.UserId != currentUserId) return;
string fullPath = Path.GetFullPath(Path.Combine(uploadsRoot, photo.StoredFileName));
if (!fullPath.StartsWith(Path.GetFullPath(uploadsRoot), StringComparison.OrdinalIgnoreCase))
    throw new InvalidOperationException("Invalid path");
if (File.Exists(fullPath)) File.Delete(fullPath);
PhotoRepo.Delete(photoId); // or mark deleted
if (photo.IsPrimary) PhotoRepo.SetAnyOtherAsPrimary(photo.UserId);

Client-side instant preview (optional) — use the FileReader API to show the selected image before upload and avoid extra round-trips. Always require server-side validation and protect delete/set-primary actions with authentication, authorization checks, POST-only endpoints, and anti-forgery tokens. Consider storing images outside the web root and serving them through an authenticated handler if access must be restricted.

Recommended Answers

All 3 Replies

First of all save that (uploaded) image file at your server machine and to show an image set ImageUrl property of Image control.

if(FileUpload1.HasFile){
    string absolute_path=MapPath("~/" + FileUpload1.FileName);
    string relative_path="~/" + FileUpload1.FileName;
    FileUpload1.SaveAs(absolute_path);
    Image1.ImageUrl=relative_path;
 }

Thanks! another thing I want to know how to code the delete button after I upload the picture if I want to delete and set as primary photo that. how can i do that?

Thanks! another thing I want to know how to code the delete button after I upload the picture if I want to delete and set as primary photo that. how can i do that?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.