日期:2014-05-18  浏览次数:20816 次

文件上传
在上传控件的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;
  }

------解决方案--------------------
IE中安全设置,上传时包含目录打钩。
------解决方案--------------------
路径 mPath = Mpage.Server.MapPath("..\\ExcelFile\\")里面的\\反了,应该是//
------解决方案--------------------
探讨

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

------解决方案--------------------
用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("/");
}