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

  • SEARCH
  • Home
  • 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 8528653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T08:51:04+00:00 2026-06-11T08:51:04+00:00

In C# ASP.Net Website, to transfer the file to client I am using String

  • 0

In C# ASP.Net Website, to transfer the file to client I am using

String file_path = Server.MapPath("~/files/"+file_name); 
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=" + file_Name);
HttpContext.Current.Response.TransmitFile(file_path);        
HttpContext.Current.Response.End();

It is working perfectly, but when the file name contains any spaces the downloaded file has a name only up to the first word. For ex: If my file name is "This is demo.txt" then the downloaded file name becomes "This" with no extension. Hence the user downloading it is not able to identify its type.
How can we avoid happening it for file name containing spaces?

I tried using

String file_path = "'"+Server.MapPath("~/files/"+file_name)+"'";

But it didn’t work.

Also its not possible for me to replace ( with ‘_’ or ‘-‘) or remove all the spaces present in the file name which are present on the server.

  • 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-06-11T08:51:05+00:00Added an answer on June 11, 2026 at 8:51 am

    You should enclose the filename in quotes.

    HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment;filename=\"" + file_Name + "\"");
    

    And if you are using VS2015 or later, you can make it a bit tidier with string interpolation:

    HttpContext.Current.Response.AddHeader("Content-Disposition", $"attachment;filename=\"{file_Name}\"");
    

    Also, the filename in the header does not have to be the same as the name of the file. The filename in the header is only a suggestion to the user.

    For full details, see RFC 6266 "Use of the Content-Disposition Header Field in the Hypertext Transfer Protocol (HTTP)," especially with regard to which characters may cause problems in a filename.

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

Sidebar

Related Questions

We have a ASP.NET website project. In the past, we had been using asmx
I have an ASP.NET website containing the following <asp:UpdatePanel ID=UpdatePanel1 runat=server > <ContentTemplate> <asp:Label
Summary: ASP.Net website with a couple hundred users. Data is exported to Excel files
I have a ASP.NET website with SQL SERVER Database. Each time database goes down
I have an existing website built on ASP.NET + SQL SERVER. It should move
My ASP.NET website has a Linq-To-Sql database connected with a connection string to an
Currently we are using query-string extensively in our asp.net application. Few days back we
I will transfer a website from CLASSIC ASP to ASP.NET. My domain name will
How to secure ASP.Net Website ( Commercial Application ) using SQL 2008 ? We
In my ASP.Net website I want to upload a file onClickEvent of LinkButton. I

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.