我必须将文件保存到物理文件夹中。但我得到了以下异常。"The path is not a virtual path“如何将物理路径更改为虚拟路径。我该如何解决这个问题呢?
Grades grade = new Grades();
grade.Exam = Exams.Search(ddlExam.SelectedValue);
grade.Person = Person.GetStudent(ddlStudent.SelectedValue);
try{
if (afuPaper.HasFile)
{
string strPath = Server.MapPath(grade.Exam.FileUrl) + ddlExam.SelectedValue +
grade.Person.TcNo + Path.GetFileName(afuPaper.FileName);
afuPaper.SaveAs(strPath); grade.GradeJpeg = strPath;
}
}发布于 2010-03-09 03:16:32
Server.MapPath需要虚拟路径并将其转换为物理路径。
假设您需要SaveAs的物理路径,那么您可以简单地删除该调用。
MapPath方法用于将虚拟路径(如"~/Location/File.aspx“)转换为物理路径(如"C:\inetput\wwwroot\MyApplication1\Location\File.aspx”
编辑:修改后的代码
try
{
if (afuPaper.HasFile)
{
string strPath = Path.Combine(grade.Exam.FileUrl, ddlExam.SelectedValue + grade.Person.TcNo + Path.GetFileName(afuPaper.FileName));
afuPaper.SaveAs(strPath);
grade.GradeJpeg = strPath;
}请注意,我还包含了对Path.Combine的调用,这是组合文件夹和文件名的首选方法(如果需要,它会在FileUrl和任何内容之间自动添加\)
https://stackoverflow.com/questions/2403710
复制相似问题