How can I delete the previous pictures which are added by current user while uploading new profile picture

StackOverflow https://stackoverflow.com/questions/23525809

  •  17-07-2023
  •  | 
  •  

Question

I'm stuck with these MVC things, How can I delete old profile picture on the upload of new one,

I'm uploading file on the base of sessionId, I know it is not a good Idea but How can I do that,

Here is the way How I upload it, BUT HOW TO DELETE ON NEW UPLOAD of file

   <script type="text/javascript">
    var uploader = new qq.FileUploader({
        // pass the dom node (ex. $(selector)[0] for jQuery users)
        element: document.getElementById('file-uploader'),
        // path to server-side upload script
        action: '/User/PictureUpload/',
        params: { sessionId: $('#sessionId').val() },
        // file extension
        allowedExtensions: ['jpg', 'jpeg', 'png', 'gif'],

        messages: {
            typeError: "{file} has invalid extension. Only {extensions} are allowed.",
            emptyError: "{file} is empty, please select files again without it.",
            allowedExtensionsError: "{file} is not allowed.",
            onLeave: "The files are being uploaded, if you leave now the upload will be cancelled."
        },
        showMessage: function (message) {
            alert(message);
        }
    });

    //Line for upstairs 
   ///// using (Html.BeginForm(null, null, FormMethod.Post, new {enctype = "multipart/form-data"}))
</script>

   [HttpPost]
    public ActionResult PictureUpload( string qqfile,string sessionId)
    {
        var path = Server.MapPath("~/App_data/Files/");
        var file = string.Empty;

        try
        {
            var stream = Request.InputStream;
            if (String.IsNullOrEmpty(Request["qqfile"]))
            {
                // IE
                HttpPostedFileBase postedFile = Request.Files[0];
                if (postedFile != null) stream = postedFile.InputStream;
                file = Path.Combine(path, sessionId);
            }
            else
            {
                //Webkit, Mozilla
                file = Path.Combine(path, sessionId );
            }

            var buffer = new byte[stream.Length];
            stream.Read(buffer, 0, buffer.Length);
            System.IO.File.WriteAllBytes(file, buffer);
        }
        catch (Exception ex)
        {
            return Json(new { success = false, message = ex.Message }, "application/json");
        }

        return Json(new { success = true }, "text/html");
    }
Était-ce utile?

La solution

you would need to know where the "old/current" picture is stored, and IF the upload succeeds, simply delete the old picture using this here:

stream.Read(buffer, 0, buffer.Length);
System.IO.File.WriteAllBytes(file, buffer);
System.IO.File.Delete(string path);

Like i said though, you'll have to know before hand what the path of the current picture is...

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top