。Save()为什么创建重复文件?

问题描述 投票:0回答:1

我的项目从用户那里获取了一个上传的文件,并从JCrop中获取了一些数据点,并将图像裁剪到所选部分。然后,为裁剪后的图像指定一个新名称,并将其保存到服务器。但是,将映像保存到服务器两次,一次使用新名称,另一次使用完全不同的名称。

CONTROLLER

[HttpPost]
[ValidateAntiForgeryToken]
[ValidImageFile]
public ActionResult _Image(ImageViewModel VM)
{
    if (ModelState.IsValid)
    {
        //Convert from HttpPostedBase to WebImage, then crop the image
        byte[] data;
        using (Stream inputStream = VM.uploadFile.InputStream)
        {
            MemoryStream memoryStream = inputStream as MemoryStream;
            if (memoryStream == null)
            {
                memoryStream = new MemoryStream();
                inputStream.CopyTo(memoryStream);                       
            }
            data = memoryStream.ToArray();
            }
        var Image = new WebImage(data);
        int T = VM.y1;
        int L = VM.x1;
        int B = Image.Height - VM.y2;
        int R = Image.Width - VM.x2;                
        Image.Crop(T, L, B, R);

        //if the user already has an image for their character, delete it.
        string oldImage = HttpContext.Server.MapPath(db.Characters.Find(VM.Id).imagePath);
        if (System.IO.File.Exists(oldImage))
        {
            System.IO.File.Delete(oldImage);
        }
        //generate random file name path, save file to server.
        var fileName = Guid.NewGuid().ToString();
        var filePath = Path.Combine("/Content/Images/charPhoto/", fileName);
        var uploadUrl = HttpContext.Server.MapPath(filePath);

       Image.Save(uploadUrl);

        //save file path to db.
        filePath = filePath + "." + Image.ImageFormat;
        db.Characters.Find(VM.Id).imagePath = filePath;
        db.SaveChanges();

        //returns the image path to jQuery.
        return Content(filePath);


    }
    return View();
}

VIEWMODEL

public class ImageViewModel
    {
        public Guid? Id { get; set; }

        public string ImagePath { get; set; }

        public HttpPostedFileBase uploadFile { get; set; }

        [Required]
        public int x1 { get; set; }
        [Required]
        public int y1 { get; set; }
        [Required]
        public int x2 { get; set; }
        [Required]
        public int y2 { get; set; }


    }

重复证明:

enter image description here

有人可以向我解释为什么会这样吗?这是WebImage的某些故意部分,用于数据备份吗?如果没有,我做错了什么,我应该怎么做?

c# image model-view-controller file-upload asp.net-mvc-5
1个回答
0
投票

OMG

    string oldImage = db.Characters.Find(VM.Id).imagePath;

    if (System.IO.File.Exists(HttpContext.Server.MapPath(oldImage)))
    {
        System.IO.File.Delete(HttpContext.Server.MapPath(oldImage));
    }

    var filePath = Path.Combine("/Content/Images/charPhoto/", Guid.NewGuid().ToString() + "." + Image.ImageFormat);

    Image.Save(HttpContext.Server.MapPath(filePath));

    //save file path to db.
    db.Characters.Find(VM.Id).imagePath = filePath;
    db.SaveChanges();

    //returns the image path to jQuery.
    return Content(filePath);
© www.soinside.com 2019 - 2024. All rights reserved.