当前位置: 代码迷 >> ASP.NET >> 怎么做到上传文件时不重名
  详细解决方案

怎么做到上传文件时不重名

热度:4820   发布时间:2013-02-25 00:00:00.0
如何做到上传文件时不重名?
我现在用的上传方法如下:
C# code
foreach(string upload in Request.Files)                {                    if (Request.Files[upload].ContentLength<=0) continue;                        string year = DateTime.Now.Year.ToString();                        string month = DateTime.Now.Month.ToString("00");                        string day = DateTime.Now.Day.ToString("00");                        //string fileFolder = string.Concat("Upload/", year, "/", month, "/", day);                        string fileFolder = string.Concat(year, "/", month, "/", day);                        string path = HostingEnvironment.MapPath("~/" + "Upload/" + fileFolder);                        if (!Directory.Exists(path))                        {                            //在Upload目录下创建了一个文件夹                               Directory.CreateDirectory(path);                        }                        string filename = Request.Files[upload].FileName;                        string fileType = Path.GetExtension(Request.Files[upload].FileName).ToLower();//取文件扩展名带"."                        string nickName = Path.GetFileName(Request.Files[upload].FileName);                        string tempfiletype = fileType.Substring(1);//取文件扩展名不带"."                        Random ran = new Random();                        string name = DateTime.Now.ToString("yyyyMMdhhmmss") + ran.Next(9999) + fileType;                        Request.Files[upload].SaveAs(Path.Combine(path, name));                        AddTempAttachment(nickName, "/" + fileFolder + "/" + name, Convert.ToInt32(Session["userid"]), tempfiletype);                }


AddTempAttachment方法的代码如下:
C# code
public void AddTempAttachment(string tempFilename, string tempPath,int tempUserID,string tempFileType)        {            tempAttachment newatt = new tempAttachment() { tempFilename = tempFilename, tempPath = tempPath, tempUerID = tempUserID, tempFileType =tempFileType };            db.tempAttachments.Add(newatt);            db.SaveChanges();        }


是用MVC3,在view中是用
C# code
@using (Html.BeginForm("AddArticle", "Admin", FormMethod.Post, new { enctype = "multipart/form-data" })){<div class="editor-label">            上传文件:        </div>        <div class="editor-field">            <input name="file1" id="file1" type="file" /><br />            <input name="file2" id="file2" type="file" /><br />            <input name="file3" id="file3" type="file" />        </div>        <p>            <input type="submit" value="添加" />        </p>}



目前,在上传文件时,会有重名现象发生。
string filename = Request.Files[upload].FileName;

  string fileType = Path.GetExtension(Request.Files[upload].FileName).ToLower();//取文件扩展名带"."
  string nickName = Path.GetFileName(Request.Files[upload].FileName);
  string tempfiletype = fileType.Substring(1);//取文件扩展名不带"."
  Random ran = new Random();
  string name = DateTime.Now.ToString("yyyyMMdhhmmss") + ran.Next(9999) + fileType;如我同时上传三个文件,就有可能发生文件重名,主要是ran.Next(9999)这里生成的随机数不会变,有时又是好的。

注:我是在本地调试时发生情况,是不是本地测试速度比较快的缘故?
  相关解决方案