首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > asp.net >

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

2012-03-29 
如何做到上传文件时不重名?我现在用的上传方法如下:C# codeforeach(string upload in Request.Files){if (

如何做到上传文件时不重名?
我现在用的上传方法如下:

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)这里生成的随机数不会变,有时又是好的。

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



所以,这个问题应该如解决掉?

[解决办法]
string name = DateTime.Now.ToString("yyyyMMdhhmmss") + ran.Next(9999) + fileType;
加上毫秒
[解决办法]
参考上传:
http://www.cnblogs.com/insus/articles/1985102.html

热点排行