日期:2014-05-20  浏览次数:20370 次

关于文件上传的问题!需要帮助!
我的所有网站文件本来都存在一个叫wp的文件夹中.这个文件夹在wwwroot/wp   后来根据需要我把所有wp文件夹里的东西都拉了出来放在了wwwroot下.结果上传文件的目录怎么改都不变.都是在wp下.源文件如下:
if(myfile.PostedFile!=null)
{
//取得文件名
string   name   =   myfile.PostedFile.FileName;
//取得最后一个 ". "
int   i   =   name.LastIndexOf( ". ");
//取得扩展名
string   newext=name.Substring(i);
//根据日期为文件命名,确定不重复
DateTime   now   =   System.DateTime.Now;
string   newname   =   now.DayOfYear.ToString()+myfile.PostedFile.ContentLength.ToString();
//保存文件到所要的目录
                                myfile.PostedFile.SaveAs(Server.MapPath( "d:\\local\\wwwroot\\ycch\\wwwroot\\mpic\\ "   +   newname   +   newext));
                                this.fname.Text   =   "d:\\local\\wwwroot\\ycch\\wwwroot\\mpic\\ "   +   newname   +   newext;
//连接数据库

}

mpic是wp下的一个文件夹.但我怎么改这个路径.不管是用根目录还是用//mpic都无法改变上传路径,总是报错说d:\\local\\wwwroot\\ycch\\wwwroot\\wp\\mpic\\   路径错误,我没有加wp啊??   怎么改不了呢??谁能帮助解决一下谢谢了!!跪求.

------解决方案--------------------

/// 获取当前站点的根路径的虚拟路径(Web方式下有效),不包括最后的路径符号。
public static string WebVirtualPath
{
get
{
if (System.Web.HttpContext.Current != null && m_strVirtualPath == null)
{
m_strVirtualPath = System.Web.HttpContext.Current.Request.ApplicationPath;
if (m_strVirtualPath != null)
{
m_strVirtualPath = m_strVirtualPath.TrimEnd(new char[] { '/ ', '\\ ' });
}
}
return m_strVirtualPath;
}

}
------解决方案--------------------
如果你把路径写死了,也就是 d:\\local\\wwwroot\\ycch\\wwwroot\\mpic\\ " + newname + newext 这种形式,就不用再 Server.MapPath Server.MapPath的作用是把相对路径如: ../aa/转换成绝对路径.
------解决方案--------------------
string str = Server.MapPath( "d:\\local\\wwwroot\\ycch\\wwwroot\\mpic\\ " + newname + newext);
然后设置断点看看路径到底是什么?