string fileName=this.upFile.PostedFile.FileName; 
string fileType; 
fileType=fileName.Substring(fileName.LastIndexOf(".")).ToUpper(); 
int intExt = fileName.LastIndexOf("."); 
string strExt = fileName.Substring(intExt); 
string strNewName = DateTime.Now.ToString("yyyyMMddhhmm").ToString() + upFile.PostedFile.ContentLength.ToString() + strExt;----------------
以上代码我想改成,不用DateTime.Now.ToString("yyyyMMddhhmm").ToString() + upFile.PostedFile.ContentLength.ToString() + strExt;
来命名,直接用原来文件的名字来命名,如何写?

解决方案 »

  1.   


    string strNewName = this.upFile.PostedFile.FileName
      

  2.   

      string fileName = Path.GetFileName(FileUpload1.PostedFile.FileName);
    直接用原来文件名容易发生重复覆盖,增加判断是否存在的功能才行。
      

  3.   

    你是把原先的文件名存到数据库里吧。
     string name=this.upFile.PostedFile.FileName;
     FileInfo file = new FileInfo(name);
     string fileName = file.Name;                 // 文件名称
     string fileend = fileName.Substring(0, fileName.LastIndexOf("."));fileend就是文件名了,已经去除了扩展名的。
    没去除扩展名的是fileName
      

  4.   

    =========================
    出现以下错误:支持给定路径的格式。 
    说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.NotSupportedException: 不支持给定路径的格式。源错误: 
    行 108: string strNewName = this.upFile.PostedFile.FileName;
    行 109: string path=Request.PhysicalApplicationPath+"wenjian/"+strNewName;
    行 110: upFile.PostedFile.SaveAs(path); 
    行 111: TextBox5.Text="wenjian/"+strNewName;
    行 112: }
     
      

  5.   

    出现以下错误
    不支持给定路径的格式。 
    行 110: upFile.PostedFile.SaveAs(path); 
      

  6.   

    楼主,我和你有同样的感觉,还好我前几天搞明白了,你看看我的代码,适当的改下名字就可以了,要是对上传的文件大小有要求的话去web.config里改下设置就好了。代码如下:
      protected void Button1_Click(object sender, EventArgs e)
        {
            if (this.FileUpload1.FileName.Length > 0)
            {            string filePath = Server.MapPath("./upload") + "\\" + this.FileUpload1.FileName;
                this.FileUpload1.SaveAs(filePath);
                Response.Write("<Script Language=JavaScript>alert(\"操作已成功\")</Script>");
            }
            else
            {
                Response.Write("<Script Language=JavaScript>alert(\"请选择上传文件\")</Script>");
            }