如何更改上传文件文件名???
比如说我上传文件为:aaa.doc
我想改成以当前时间命名的:200807221307.doc而且还要改好后保存到数据库中去
以便下次可以直接调用

解决方案 »

  1.   

    string FileName = Server.MapPath("./" + DateTime.Now.ToString("yyyyMMddHHmmss.doc"));
    FileUpload1.SaveAs(FileName);
      

  2.   

    public static string fileUpload(System.Web.UI.WebControls.FileUpload theFileUpload, string path, System.Web.UI.Page thePage, bool isShow)
            {
                string[] returnStr = new string[3];
                if (theFileUpload.HasFile)
                {
                    string fileContentType = theFileUpload.PostedFile.ContentType;
                    if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg")
                    {
                        string name = theFileUpload.PostedFile.FileName;                                                   //客户端文件路径                     FileInfo file = new FileInfo(name);
                        string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + System.IO.Path.GetExtension(file.Name);//文件名称 
                        string largerPath = thePage.Server.MapPath(path + "larger/" + fileName);                           //服务器端文件路径 
                        if (!File.Exists(largerPath))
                        {
                            theFileUpload.SaveAs(largerPath);                                                            //使用SaveAs方法保存文件
                            return fileName;
                        }
                    }
                }
                return "";
            }
      

  3.   

    string fileName= FileUpload1.FileName;   //得到上传文件的名字
    //得到文件的扩展名
    string fileType = fileName.Substring(fileName.LastIndexOf(".") + 1);
    --你可以把他名字改掉(比如加上时间);
    fileName=fileName+DateTime.Now.ToString();
    //保存文件路径
    string iPath = Server.MapPath("要保存的虚拟目录") + "\\" + fileName;
    //保存文件
    FileUpload1.SaveAs(iPath);
      

  4.   

    谢谢两位了,受你们的启示,我已经搞定了
    其实不用那么复杂,我把我刚做的贴出来看看: void UpPic()
            {
                string filename = FileUpload1.FileName;
                int posdot = filename.LastIndexOf(".");
                string extname = filename.Substring(posdot + 1);
                string[] good = new string[] { "jpg", "gif", "bmp", "jepg","doc","txt" };
                int i = 0;
                foreach (string a in good)
                {
                    if (a == extname.ToLower())
                    {
                        i++;
                    }
                }            if (i > 0)
                {
                    string aa = filename.Substring(0, posdot);
                    filename=filename.Replace(aa,DateTime.Now.ToString("yyyyMMddHHmmss"));
                    string paths = Server.MapPath("\\") + "File\\";
                    strphoto = paths + filename;
                    FileUpload1.SaveAs(strphoto);
                }
            }