protected void btnImport_Click(object sender, EventArgs e)
{
if (file1.PostedFile.ContentLength > 0)
{
//连接excel数据源
string fileName = file1.PostedFile.FileName;
file1.PostedFile.SaveAs(Server.MapPath("../Upload/" + fileName));//上传文件到网站上upload目录
string excelconnstring = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + Server.MapPath("../Upload/" + fileName) + "";));
excelconnstring += @";Extended Properties=""Excel 8.0;HDR=Yes;IMEX=1""";//读取上传文件
OleDbConnection excelCon = new OleDbConnection(excelconnstring);
string sqlStr = "select * from [sheet1$]";
OleDbDataAdapter da = new OleDbDataAdapter(sqlStr, excelCon); DataTable dt = new DataTable();
da.Fill(dt); try
{
for (int i = 4; i < dt.Rows.Count;i++){
//这里面都是参数赋值,没东西
}
MBx.M_IconType = Icon_Type.OK;
MBx.M_Title = "操作成功!";
MBx.M_Body = string.Format("上传报表信息成功!");
MBx.M_WriteToDB = true;
MBx.M_ButtonList.Add(new sys_NavigationUrl("确定", Common.GetHomeBaseUrl("EightTotalInfo.aspx"), "确定!", UrlType.Href, true));
}
catch (Exception ex)
{
MBx.M_IconType = Icon_Type.Error;
MBx.M_Title = "操作失败!";
MBx.M_Body = string.Format("上传报表信息失败!");
MBx.M_WriteToDB = true;
MBx.M_ButtonList.Add(new sys_NavigationUrl("确定", Common.GetHomeBaseUrl("EightTotalInfo.aspx"), "确定!", UrlType.Href, true));
}
finally
{
EventMessage.MessageBox(MBx);
}
}
else
{
MBx.M_IconType = Icon_Type.Error;
MBx.M_Title = "操作失败!";
MBx.M_Body = string.Format("请选择上传的文件信息");
MBx.M_WriteToDB = true;
MBx.M_ButtonList.Add(new sys_NavigationUrl("确定", Common.GetHomeBaseUrl("EightTotalInfo.aspx"), "确定!", UrlType.Href, true));
EventMessage.MessageBox(MBx);
}
}以上代码本地调试,本地IIS访问均没有问题,别人远程访问IIS,告诉我什么路径不对
D;/project/web/f:/...什么的不是一个正确的虚拟路径,我也呐闷了,没问题啊
解决办法:你可以先把excel上传到你的服务器上,导入成功后,在把该excel文件从服务器删除
路径是否存在
Server.MapPath的用法:
1.Server.MapPath("/") 应用程序根目录所在的位置 如 C:\Inetpub\wwwroot\
2.Server.MapPath("./") 表示所在页面的当前目录
注:等价于Server.MapPath("") 返回 Server.MapPath("")所在页面的物理文件路径
3.Server.MapPath("../")表示上一级目录
4.Server.MapPath("~/")表示当前应用级程序的目录,如果是根目录,就是根目录,如果是虚拟目录,就是虚拟目录所在的位置 如:C:\Inetpub\wwwroot\Example\
注:等效于Server.MapPath("~")。 看清楚自己需要的是哪个位置,看看是否这个原因出问题
file1.PostedFile.SaveAs(Server.MapPath("~/Upload/" + fileName));就可以了?或者说 file1.PostedFile.SaveAs(Server.MapPath("../Upload/" + fileName));远程访问得到的是全路径,而不是相对路径,我调试进入后string fileName = file1.PostedFile.FileName;
得到的文件名,不是全路径,而远程访问得到的全路径,是这个问题吧