0

我正在使用以下代码将图像上传到我的网站,图像上传很好,但是,我需要修复以下问题:-我得到了这种 Url C:\Users\Me\Documents\Visual Studio 2013\Projects\Wow\WowMvc5\WowMvc5\images\gallery\Picture 022.jpg,而不是相关文件夹 Url -I 为了避免 2 张同名图像的错误,最好在下创建一个文件夹每张图片的图片(或任何更好的想法)谢谢您的时间

        public async Task<ActionResult> Create([Bind(Include = "TakeAwayId,TakeAwayName,description,Price,DishUrl,quantity,DishesAmount,GenreId")] TakeAway takeaway)
    {
        var path = Server.MapPath("~/Images/gallery/");
        foreach (string item in Request.Files)
        {
            HttpPostedFileBase file = Request.Files[item];
            if (file.ContentLength == 0)
            {
                continue;
            }

            string SavedFileName = System.IO.Path.GetFileName(file.FileName);
            SavedFileName = Server.MapPath
                ("~" + "/images/gallery/" + SavedFileName);

            file.SaveAs(SavedFileName);
            takeaway.DishUrl = SavedFileName;
        }
        if (ModelState.IsValid)
        {
            db.takeaway.Add(takeaway);
            await db.SaveChangesAsync();
            return RedirectToAction("Index");
        }

        ViewBag.GenreId = new SelectList(db.genre, "GenreId", "GenreName", takeaway.GenreId);
        return View(takeaway);
    }
4

2 回答 2

0

我要做的是使用HashCode命名并保存每张图片。

根据定义,两个不同的字符串在使用哈希算法进行转换时,不太可能具有相同的输出可以肯定的是,在图像的原始名称中添加一个随机字符串。

string newName = (oldName + random).GetHashCode().ToString()
于 2013-10-30T09:13:43.123 回答
0

filename_1.jpg 其中 cnt 是增量计数 if(file.exists() { string cnt = file.split(" "); String newFileName = filename+" "+(cnt+1)+".jpg"; }

于 2013-10-30T09:29:37.867 回答