如何从服务器系统下载txt文件到客户端?

时间:2014-02-24 12:37:28

标签: c# asp.net

 <%@ WebHandler Language="C#" Class="dlde" %>

using System;
using System.Web;

public class dlde : IHttpHandler {

public void ProcessRequest (HttpContext context) {
    var fileName = @"D:\Error.txt";
    var r = context.Response;
    r.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
    r.ContentType = "text/plain";
    r.WriteFile(context.Server.MapPath(fileName));
}

public bool IsReusable {
    get {
        return false;
    }
}

}

我想从指定目录下载服务器系统中的.txt文件...我尝试使用此代码,但我收到错误,虚拟路径无效。如何从服务器系统获取文件。 。请帮助我...

3 个答案:

答案 0 :(得分:0)

使用"WebClient "

尝试此操作
     string remoteUri = "http://Yoursite.com/file.txt";
     string fileName = @"D:\";
      WebClient myWebClient = new WebClient();
      myWebClient.DownloadFile(remoteUri, fileName);

MSDN学习命名空间

答案 1 :(得分:0)

 string fName = @"D:\Error.txt";

 System.IO.FileStream fs = System.IO.File.Open(fName, System.IO.FileMode.Open);
 byte[] btFile = new byte[fs.Length];
 fs.Read(btFile, 0, Convert.ToInt32(fs.Length));
 fs.Close();

 context.Response.Clear();
 context.Response.ClearHeaders();
 context.Response.AddHeader("Content-disposition", "attachment; 
           filename=" + HttpUtility.UrlEncode(fname, System.Text.Encoding.UTF8));
 context.Response.ContentType = "text/plain";  
 context.Response.BinaryWrite(btFile);           

答案 2 :(得分:0)

drawDonut(data1, "#pie1", "Chart 1")

...

function drawDonut(data, divchart, chartName) {
  ...
  g.append("text")
    .attr("transform", "translate(0,60) scale(.7)")
    .attr("class", "caption")
    .text(chartName);
}