Click here to Skip to main content
14,638,900 members
Rate this:
Please Sign up or sign in to vote.
See more:
<%@ 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;
        }
    }

}



I want to download the .txt file from the server system from the specified directory...i tried with this code,but i am getting error,invalid virtual path..How to get the file ,from the server system ..please help me...
Posted
Comments
ravikhoda 24-Feb-14 7:43am
   
try server.mappath and not the actual path.

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

First your file should be a virtual path relative to your IIS location, like: "\\Content\\Doc\\Error.txt", and the file must exist with this name on your web Server.
Then you should invoke r.End(); after r.WriteFile(...);
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100