如果文件全部存在,如何创建新文件夹?

编程入门 行业动态 更新时间:2024-10-23 14:20:02
本文介绍了如果文件全部存在,如何创建新文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在上传图片,但是如果某个图片同名,那么图片覆盖... !!那我该怎么办? 我可能需要创建新文件夹才能存储此图像吗?或者其他可能解决方案? 如果我必须创建新文件夹,那么这个怎么可能我要编写什么代码? 我尝试过: [HttpPost] 公共ActionResult索引(上传上传,HttpPostedFileBase文件) { if(File == null) { ModelState .AddModelError(customeerror,请上传你的文件); } try { //字符串文件名= Guid.NewGuid()+ Path.GetExtension(File.FileName); string filename = Path.GetFileName (File.FileName); File.SaveAs(Path.Combine(Server.MapPath(〜/ uploadcv),filename)); using(NGOEntities1 db =新的NGOEntities1()) { upload.resum e =文件名; db.uploads.Add(上传); db.SaveChanges(); } ModelState.Clear(); upload = null; ViewBag.message =Succcess; } catch(例外情况) { ViewBag.message =错误; 返回查看(); } 返回RedirectToAction(list); }

i am uploading image but if some image with same name then image override ...!! so what can i do ?? may i have to create new folder to store this image ? or what other possibly solution ?? if i have to create new folder then how this posible what code i have to write ?? What I have tried: [HttpPost] public ActionResult Index(upload upload,HttpPostedFileBase File) { if (File == null) { ModelState.AddModelError("customeerror", "Please Upload Your file"); } try { // string filename = Guid.NewGuid() + Path.GetExtension(File.FileName); string filename = Path.GetFileName(File.FileName); File.SaveAs(Path.Combine(Server.MapPath("~/uploadcv"), filename)); using (NGOEntities1 db = new NGOEntities1()) { upload.resume = filename; db.uploads.Add(upload); db.SaveChanges(); } ModelState.Clear(); upload = null; ViewBag.message = "Succcess"; } catch(Exception Ex) { ViewBag.message = "Error"; return View(); } return RedirectToAction("list"); }

推荐答案

使用 File.Exists [ ^ ]查看文件是否已存在,如果它确实改变它就像在最后添加一个数字,然后再次检查,依此类推,直到你提交一个不存在的文件。 承担考虑到人们同时上传,你可能想要围绕这段代码实现锁定。 Use File.Exists[^] to see if the file already exists, and if it does alter it like adding a number to the end, then checking again and so on until you file a file that doesn't exist. Bear in mind people uploading at the same time though, you might want to implement locking around this code.

更多推荐

如果文件全部存在,如何创建新文件夹?

本文发布于:2023-10-30 19:27:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1543781.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件夹   文件

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!