Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6803821
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:23:27+00:00 2026-05-26T19:23:27+00:00

i have a handler for download files like below : using System; using System.Collections.Generic;

  • 0

i have a handler for download files like below :

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using NiceFileExplorer.Classes;

namespace NiceFileExplorer
{
    /// <summary>
    /// Summary description for HandlerForMyFE
    /// </summary>
    public class HandlerForMyFE : IHttpHandler, System.Web.SessionState.IRequiresSessionState
    {
        private HttpContext _context;
        private HttpContext Context
        {
            get
            {
                return _context;
            }
            set
            {
                _context = value;
            }
        }

        public void ProcessRequest(HttpContext context)
        {
            Context = context;
            string filePath = context.Request.QueryString["Downloadpath"];
            filePath = context.Server.MapPath(filePath);

            if (filePath == null)
            {
                return;
            }

            System.IO.StreamReader streamReader = new System.IO.StreamReader(filePath);
            System.IO.BinaryReader binaryReader = new System.IO.BinaryReader(streamReader.BaseStream);

            byte[] bytes = new byte[streamReader.BaseStream.Length];

            binaryReader.Read(bytes, 0, (int)streamReader.BaseStream.Length);

            if (bytes == null)
            {
                return;
            }

            streamReader.Close();
            binaryReader.Close();

            string fileName = System.IO.Path.GetFileName(filePath);
            string MimeType = GetMimeType(fileName);
            string extension = System.IO.Path.GetExtension(filePath);
            char[] extension_ar = extension.ToCharArray();
            string extension_Without_dot = string.Empty;
            for (int i = 1; i < extension_ar.Length; i++)
            {
                extension_Without_dot += extension_ar[i];
            }

            string filesize = string.Empty;
            FileInfo f = new FileInfo(filePath);
            filesize = f.Length.ToString();

            if (HttpContext.Current.Session["User_ID"] != null)
            {
                WriteFile(bytes, fileName, filesize, MimeType + " " + extension_Without_dot, context.Response);
            }

        }

        private void WriteFile(byte[] content, string fileName, string filesize, string contentType, HttpResponse response)
        {
            response.Buffer = true;
            response.Clear();

            response.ContentType = contentType;

            response.AddHeader("content-disposition", "attachment; filename=" + fileName);

            response.AddHeader("Content-Length", filesize);

            response.BinaryWrite(content);
            response.Flush();
            response.End();
        }

        private string GetMimeType(string fileName)
        {
            string mimeType = "application/unknown";
            string ext = System.IO.Path.GetExtension(fileName).ToLower();
            Microsoft.Win32.RegistryKey regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext);
            if (regKey != null && regKey.GetValue("Content Type") != null)
                mimeType = regKey.GetValue("Content Type").ToString();
            return mimeType;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

the important part of this handler is WriteFile And It works perfect!
i call this handler for download a file from code behind like below :

Response.Redirect("~/Handler.ashx?Downloadpath=" + HttpUtility.UrlEncode(DownloadPath));

one of my download links in my web site is like below :

http://localhost:5410/en/Download.aspx?Downloadpath=%2fFiles%2f%2fsamsung%2fGE2550_DEFAULT_MDL_V002.exe

so, i can contorl my download links easily by that handler!

my problem is when some body changes that link to :

http://localhost:5410/Files/samsung/GE2550_DEFAULT_MDL_V002.exe  

can download that file directly without that handler!

how can i prevent this direct download?

thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T19:23:28+00:00Added an answer on May 26, 2026 at 7:23 pm

    First, putting the actual physical path to the file into the querystring is not really a good idea. Gives a bit too much information to the public, and opens you to security issues with people putting unexpected paths into the url to try and download other files.

    With that said, regarding you problem above, you should either put your Files folder outside of the web root so that it is inaccessible from the browser, or setup IIS so that no one is allowed access to that folder (and subfolders). As long as the account that ASP.NET runs under has permissions to the folder, you will still be able to open the file in your code and write it to the response, regardless of whether it’s visible through IIS.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple 'file download' generic handler which sets the response contenttype and
I'm using a handler(.ashx) to serve some files. I have a folder where I
I have to download some files from an FTP server. Seems prosaic enough. However,
We have created a web application, using ASP.NET, that allows users to upload documents
I have a problem when i try to get the direct download link using
I have a ashx handler that gets a file we are using, this in
I am making an iPad app where you can download files (like PDF, doc,
Hello i did my notification of download file from web. Code below. I want
We have a library of ZIP files that we would like to make available
i have a file explorer like below : <%@ Page Language=C# AutoEventWireup=true CodeBehind=SearchBar.aspx.cs Inherits=FileExplorer.SearchBar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.