读书人

从本机获取文件下传到服务器下取不到

发布时间: 2013-01-28 11:49:56 作者: rapoo

求助,从本机获取文件上传到服务器上取不到正确路径问题
protected void FileUpload()
{
string webPathFile = Server.MapPath(Request.Url.AbsolutePath);//该处始终取得不到服务器的正确路径,求解???

webPath = webPathFile.Substring(0, webPathFile.LastIndexOf("\\") + 1) + "EXCEL" + "\\";


for (int x = 0; x <ListExcelName.Items.Count;x++ )
{
FileUpload(ListExcelName.Items[x].Text.ToString(), webPath);
}

}
public void FileUpload(string localPathFile, string webPhysicalPathFile)//localPathFile是本地物理文//件 webPhysicalPathFile是服务器物理路径
{

FileStream inStream = null;

FileStream outStream = null;

try
{

string fileName = localPathFile.Substring(localPathFile.LastIndexOf("\\") + 1);

//读取本地文件流

inStream = File.OpenRead(localPathFile);

string file = webPhysicalPathFile + fileName;//得到网络服务器的文件路径名;

outStream = new FileStream(file, FileMode.OpenOrCreate, FileAccess.Write);

byte[] bytes = new byte[4096];

int start = 0;

int length;

while ((length = inStream.Read(bytes, 0, 4096)) > 0)
{
outStream.Write(bytes, 0, length);

start += length;



}

inStream.Close();

outStream.Close();
}

catch (Exception e)
{

throw new Exception("错误出现在:" + e.Message);

inStream.Close();

outStream.Close();
}

finally
{

inStream.Close();

outStream.Close();
}

}
[解决办法]
怎么个不正确发?
[解决办法]
没有文件....
[解决办法]
为什么要取服务器的物理路径,用它的相对路径就可以了。
[解决办法]
Request.Url.AbsolutePath是Http://****.***.**/****.txt这样的路径吧。Server.MapPath不接收这样的参数的

*********************************************************
老是忘记Server.MapPath的使用方法了,下面记录一下,以备后用:
总注:Server.MapPath获得的路径都是服务器上的物理路径,也就是常说的绝对路径
1、Server.MapPath("/")
注:获得应用程序根目录所在的位置,如 C:\Inetpub\wwwroot\。
2、Server.MapPath("./")
注:获得所在页面的当前目录,等价于Server.MapPath("")。
3、Server.MapPath("../")
注:获得所在页面的上级目录。
4、Server.MapPath("~/")
注:获得当前应用级程序的目录,如果是根目录,就是根目录,如果是虚拟目录,就是虚拟目录所在的位置,如C:\Inetpub\wwwroot\Example\。
*********************************************************
来自:
http://www.cnblogs.com/Showshare/archive/2007/04/23/723965.html

读书人网 >asp.net

热点推荐