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();
} }
{
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();
} }
解决方案 »
- MVC中使用 DropDownlist
- new JavaScriptSerializer().Serialize(result)速度很慢?
- 刚上线的csdn深圳站是.net和php双语言的吗?
- 帮我改一个正则表达式,多项匹配的问题
- 通用分页显示查询存储过程(献给朋友们)
- 写了很多的dropdownlist嵌入datagrid,今天居然会这样~~思考不出原因来了
- 我散分,你来接吧
- 想生成TreeView,不知为什么会出错(DataReader)
- 求救:SSL问题
- 全局变量赋值问题
- .net mvc中System.Web.Helpers.WebImage的resize方法
- 我要实现这样的gridview
老是忘记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