日期:2014-05-17  浏览次数:20404 次

ASP.NET 如何实现读取客户端C盘的某个文本文件?
ASP.NET 如何实现读取客户端C盘的某个指定文本文件? 路径指定的

------解决方案--------------------
web模式的获取不到客户端文件的,浏览器就是这样设计的
------解决方案--------------------
所以只能用上传控件上传到服务器处理
------解决方案--------------------
除非是winform程序,在web中是不可能实现的。要不没安全可言
------解决方案--------------------
你怎么知道客户端有C盘?
------解决方案--------------------
获取文件名 然后通过流的方式读取 比如下面的一个导入txt文件的例子
JScript code

//ajax方式提交到FileImport.ashx
function upLoadFile() 
{
   if ($("#<%=File1.ClientID%>").val() != "") 
   {
      var id = $("#<%=ChannelID.ClientID%>").val();
      var options = {
        type: "POST",
        url: 'FileImport.ashx?action=' + id,
        beforeSubmit: showRequest,
        success: showResponse
      };
      $('#myForm').ajaxSubmit(options);
   }
   else 
   {
     alert("请选择需要导入的节目单文件!");
   }
}
//FileImport.ashx读取txt文件内容
HttpFileCollection files = context.Request.Files;
if (files.Count > 0)
{
   string id=context.Request.QueryString["action"];
   HttpPostedFile file = files[0];
   int FileLen = file.ContentLength;
   byte[] input = new byte[FileLen];
   System.IO.Stream uploadStream = file.InputStream;
   uploadStream.Read(input, 0, FileLen);
   uploadStream.Position = 0;
   System.IO.StreamReader sr = new System.IO.StreamReader(uploadStream, Encoding.GetEncoding("gb2312"));
   string line;
   while ((line = sr.ReadLine()) != null)
   {
      //domething 
   }
}