首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

文件上传,该怎么解决

2012-05-16 
文件上传在上传控件的onchange事件中用一个textbox记录下上传的路径:function SelectValue(obj){if (obj){

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


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)
{

}
}
}

热点排行