Why is the file header incomplete if it has space in ASP.NET MVC 3?

Today I met a problem, this is strange for me, but maybe not for experts in the C # field.

I have a function called Download like this (piece of code!)

 public void Download (string path){ HttpContext.Current.Response.ContentType = "application/octet-stream"; try { ....//process a 'filePath' variable using the 'path' parameter using ( FileStream sourceFile = new FileStream( filePath, FileMode.Open ) ) { ... HttpContext.Current.Response.AddHeader( "Content-Disposition", "attachment; filename=" + Path.GetFileName( filePath ) ); HttpContext.Current.Response.AddHeader( "Content-Length", fileSize.ToString() ); HttpContext.Current.Response.BinaryWrite( getContent ); } ... } 

If the file name specified and stored in the path / filePath variable contains a space, for example

PR SimpleTest.xls

then the file name, for example PR , is indicated in the download field, and nothing more.

enter image description here

If this name has NO space (for example, PR_SimpleTest.xls ), the header comes with PR_SimpleTest.xls and I can load it as such (the full file name appears with its extension).

Is there a solution to solve the problem if the file name contains spaces?

+4
source share
4 answers

A google search of the headers in the http headers finds this knowledge base article asking you to enter the file name in quotation marks. For instance.

 HttpContext.Current.Response.AddHeader( "Content-Disposition", "attachment; filename=\"" + Path.GetFileName( filePath ) + "\""); 
+11
source

The answer is above using:

Server.UrlEncode (strFileName)

didn't work for me, this led to a file name that was:

"My + Long + Filename.txt"

whereas the first solution using the surrounding quotation file name produces:

"My Long Filename. Txt"

what do we want.

+1
source

I think you are using Firefox. Take a look at this link: Downloading a file problem

 Response.AppendHeader("content-disposition", "attachment; filename=" + Server.UrlEncode(strFileName)) 

Also see ASP.NET MVC Upload and Download Files and Writing the Result of an Action to Upload a Custom File for ASP.NET MVC . In MVC, you can use special actions.

0
source

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

0
source

Source: https://habr.com/ru/post/1435196/


All Articles