Wednesday, March 24, 2010

Write Files directly on Response ASP.Net C#


using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.IO;

///
/// Summary description for Util
///
namespace Utility
{
    public class FileUtil
    {
        public FileUtil()
        {
            //
            // TODO: Add constructor logic here
            //
        }
        static public void writeFileToResponse(string filePath,HttpResponse Response)
        {
            try
            {
           
            string FileName = Path.GetFileName(filePath);
            Response.Clear();

            Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName);

            //Response.ContentType = sContentType;

            Response.WriteFile(filePath);

            Response.Flush();

            File.Delete(filePath);
            Response.End();

            }
            catch (System.Exception ex)
            {

            }

        }
    }
}

1 comment:

  1. It is a great piece of code, we can segregate download able files from virtual directory, for security propose, using this code we can avoid a download similar to Rapid Share) link, nobody can access any file from URL.

    Great post! Thanks Asad

    ReplyDelete

Followers

Search This Blog