文件上传

qinzhuhua 2012-05-10 11:02:53
在上传控件的onchange事件中用一个textbox记录下上传的路径:
function SelectValue(obj){
if (obj)
{
if (window.navigator.userAgent.indexOf("MSIE") >= 1)
{
obj.select();
document.getElementById("txtUp").value=document.selection.createRange().text;
}
else
{
document.getElementById("txtUp").value=obj.value;
}
}
}

在以下函数中,出现“找不到D:xx.xls文件”,路径中的"\"怎么不见了?我点击浏览后,textbox中显示的路径是对的,
为什么导入的时候出这个错?而且系统发布后,在我们公司的局域网中是没问题的,怎么一到客户那去就出现这个问题?
public string LoadFile2(Page Mpage, string filePath)//filePath为textbox值
{
string fileExtName = "", mFileName = "", mPath = "", res = "";
try
{
if (filePath != "")
{
fileExtName = filePath.Substring(filePath.LastIndexOf("."));
if (fileExtName.ToLower() == ".xls")
{
mPath = Mpage.Server.MapPath("..\\ExcelFile\\");//取得与 Web 服务器上的指定虚拟路径相对应的物理文件路径。
mFileName = DateTime.Now.ToString("yyyyMMddHHmmss") + ".xls";//设置上传文件名
File.Copy(filePath, mPath + mFileName, false);
res = mPath + mFileName;
}
else
{
Mpage.ClientScript.RegisterStartupScript(typeof(string), "", "<script>alert('只能导入Excel文件!')</script>");
}
}
else
{
Mpage.ClientScript.RegisterStartupScript(typeof(string), "", "<script>alert('请选择需导入的文件!')</script>");
}
}
catch (Exception ex)
{
Mpage.ClientScript.RegisterStartupScript(typeof(string), "", "<script>alert('"+ex.Message+"')</script>");
}
return res;
}
...全文
89 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
coolsz 2012-05-10
  • 打赏
  • 举报
回复
用form表单提交
public void UploadDoc(string filename)
{
string Durl = string.Empty;
HttpFileCollection files = Request.Files;
if (files != null && files.Count > 0)
{
HttpPostedFile file = files[0];
Durl = file.FileName;

Users user = GetUser((int)Session["UserID"]);

string fileName = Durl;
string Po = fileName.Substring(fileName.LastIndexOf(".") + 1).Trim().ToLower();//得到文件的扩展名

int indexOf = 0;
if (Durl.Contains(@"\"))
{
indexOf = Durl.LastIndexOf(@"\");
}
else if (Durl.Contains("/"))
{
indexOf = Durl.LastIndexOf("/");
}

string filetxt = Durl.Substring(indexOf + 1);

string tmpPath = string.Empty;

filetxt = user.UserName + "_UserImg_" + DateTime.Now.ToString("yyyyMMddHHmmss") + "." + Po;
tmpPath = System.Web.HttpContext.Current.Server.MapPath("~/UpFile/UserImg/");

//判断文件夹是否存在,不存在则创建
if (!Directory.Exists(tmpPath))
{
Directory.CreateDirectory(tmpPath);
}

try
{
file.SaveAs(tmpPath + filetxt);
}
catch (Exception err)
{

}
}
}
指间的风 2012-05-10
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 的回复:]

路径 mPath = Mpage.Server.MapPath("..\\ExcelFile\\")里面的\\反了,应该是//
[/Quote]


fuermoshia 2012-05-10
  • 打赏
  • 举报
回复
路径 mPath = Mpage.Server.MapPath("..\\ExcelFile\\")里面的\\反了,应该是//
qinzhuhua 2012-05-10
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 的回复:]
IE中安全设置,上传时包含目录打钩。
[/Quote]

打了勾
test2050 2012-05-10
  • 打赏
  • 举报
回复
IE中安全设置,上传时包含目录打钩。

110,534

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧