//保存文件
string filepath = Server.MapPath("file/"+Path.GetFileName(pic.PostedFile.FileName)); 
int flag = filepath.LastIndexOf(".");
filepath = Server.MapPath("file/"+DateTime.Now.Year+DateTime.Now.Month+DateTime.Now.Day+DateTime.Now.Hour+DateTime.Now.Minute+DateTime.Now.Second+filepath.Substring(flag)); 
 
int i=0;
while(System.IO.File.Exists(filepath))
{
i++;
int index = filepath.LastIndexOf(".");
filepath = filepath.Substring(0,index) + i.ToString() + filepath.Substring(index);
}

if(pic.PostedFile != null)
{
try
{  
pic.PostedFile.SaveAs(filepath);  
}  
catch (Exception exc)
{  
//Span1.Text = "保存文件时出错<b>" + filepath + "</b><br>"+ exc.ToString();  
}
}

//保存文件结束我是将filepath 保存到数据库里面去的,但是发现保存的是绝对路径,我希望是相对路径,我应该怎么做??

解决方案 »

  1.   

    你应该保存的是你保存在服务器上的文件名,可是你保存的是路径。
    filepath = DateTime.Now.ToString("yyyyMMddhhmmss")+filepath.Substring(flag); 
      

  2.   

    楼上的大哥,
    这样的话保存的路径就不正确了,我希望是保存在file下面
      

  3.   


    string filename=pic.PostedFile.FileName;
    int j=filename.LastIndexOf("\\");
    //获取上传的文件名name
    string name=filename.Substring(j+1);
      

  4.   

    后面加上
    filepath = Server.MapPath("file/"+DateTime.Now.Year+DateTime.Now.Month+DateTime.Now.Day+DateTime.Now.Hour+DateTime.Now.Minute+DateTime.Now.Second+name;