如何删除当前用户在上传新个人资料照片时添加的上一张照片

时间:2014-05-07 18:44:19

标签: c# asp.net-mvc asp.net-mvc-5.1

我坚持使用这些MVC的东西,如何删除上传新的个人资料图片,

我在sessionId的基础上上传文件,我知道这不是一个好主意,但我怎么能这样做,

以下是我如何上传它的方式,但是如何在文件的新上传中删除

   <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");
    }

1 个答案:

答案 0 :(得分:0)

您需要知道“旧/当前”图片的存储位置,如果上传成功,只需在此处删除旧图片:

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

就像我说的那样,你必须事先知道当前图片的路径是什么......