0

In my c# code, I am trying to get filename from content disposition as the content type is application/octet-stream.

But for some urls, the content disposition filename contains spaces or non ascii characters. It causes following exception while creating new content dispostion object.

System.FormatException: An invalid character was found in the mail header: 'R'. at System.Net.Mime.ContentDisposition.ParseValue()

System.FormatException: The specified content disposition is invalid. at System.Net.Mime.ContentDisposition.ParseValue() at System.Net.Mime.ContentDisposition..ctor(String disposition) at Rextester.Program.Main(String[] args)

System.FormatException: An invalid character was found in the mail header: 'Ã'. at System.Net.Mime.MailBnfHelper.ReadQuotedString(String data, Int32& offset, StringBuilder builder, Boolean doesntRequireQuotes, Boolean permitUnicodeInDisplayName) at System.Net.Mime.ContentDisposition.ParseValue()

System.FormatException: The specified content disposition is invalid. at System.Net.Mime.ContentDisposition.ParseValue() at System.Net.Mime.ContentDisposition..ctor(String disposition) at Rextester.Program.Main(String[] args)

Are these errors expected? I read somewhere that it only expects ascii characters. But I am facing issues even in case filename has spaces.

What can be alternative to this? I need to know the extension solely by utilizing the response header.

mjwills
  • 23,389
  • 6
  • 40
  • 63
andor
  • 75
  • 8
  • Can you share an example of such a URL that causes the issue? – mjwills Jun 13 '18 at 12:08
  • http://investors.mzb-group.com/fase2/php/fileDownload.php?id=697 https://www.iochpe.com.br/Download.aspx?Arquivo=X+zZd3KGiDkpKELug+COUA%3D%3D – andor Jun 13 '18 at 12:26
  • No. I am trying to get the extension of the content disposition filename for some purpose. – andor Jun 13 '18 at 12:31
  • The value you are receiving is `"attachment; filename=Press Release FY2017 Results.pdf"` It is invalid since there is a space in the filename. You will need to 'clean' the data (i.e. add quotes around the filename) as per https://stackoverflow.com/a/38324508/34092 . – mjwills Jun 13 '18 at 12:36

0 Answers0