Encoding ASP.NET file name when sending a file

I am sending a file from an ASP.NET page to a browser. To send the file name correctly, I add the header:

Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=" + filename);

The problem is that when the file contains spaces (for example, "abc def"), the browser receives only part of the "abc" file name. I tried with: Server.HtmlEncode, but that did not help.

Do you have any ideas how to solve this problem?

PC

+3
source share
4 answers

Put the file name in quotation marks: -

Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
+9
source

UrlEncode. HTTP. IE - , .

, (+1). Content-Disposition , , , ;, " Unicode. , , - URI URL- UTF-8 +.

.

+4

(: ; # @! $) - , . chanext, : ( #), , (IE, Opera, Firefox Chrome) "Microsoft.Asp.Net.doc" "F ile;;! @% # ^ & y.doc" , , .

http://ciznx.com/post/aspnetstreamdownloaddisplaynonunicodespacechar.aspx

+1

Based on the code referenced by @chanext, I cleaned it up and put it in one extension method. Hope this helps someone.

Partial Class Uploader
  Inherits Page

  Private Sub UploadFile()
    Dim sFileName As String
    Dim oPdf As MigraDoc.Rendering.PdfDocumentRenderer

    sFileName = "File Name With Spaces #22.pdf"

    With Me.Request.Browser
      If .Browser = "InternetExplorer" OrElse .Browser = "IE" Then
        sFileName = sFileName.EncodeForIE
      Else
        sFileName = String.Format("""{0}""", sFileName)
      End If
    End With

    oPdf = New MigraDoc.Rendering.PdfDocumentRenderer
    oPdf.Document = FileFactory.CreatePdf()
    oPdf.RenderDocument()

    Using oStream As New MemoryStream
      oPdf.Save(oStream, False)

      Me.Response.Clear()
      Me.Response.ContentType = "application/pdf"
      Me.Response.AddHeader("content-disposition", String.Format("attachment; filename={0}", sFileName))
      Me.Response.AddHeader("content-length", oStream.Length)
      Me.Response.BinaryWrite(oStream.ToArray)
    End Using

    Me.Response.Flush()
    Me.Response.End()
  End Sub
End Class



Public Module StringExtensions
  <Extension()>
  Public Function EncodeForIE(Url As String) As String
    Dim _
      sReservedChars,
      sEncodedString As String

    sReservedChars = "$-_.+!*'(),@=&"

    With New StringBuilder
      Url.ToList.ForEach(Sub(C)
                           If Char.IsLetterOrDigit(C) OrElse sReservedChars.Contains(C) Then
                             .Append(C)
                           Else
                             With New StringBuilder
                               C.ToBytes.ToList.ForEach(Sub(B)
                                                          .AppendFormat("%{0}", Convert.ToString(B, 16))
                                                        End Sub)

                               sEncodedString = .ToString
                             End With

                             .Append(sEncodedString)
                           End If
                         End Sub)

      Return .ToString
    End With
  End Function

  <Extension()>
  Public Function ToBytes(Chr As Char) As Byte()
    Return Encoding.UTF8.GetBytes(Chr.ToString)
  End Function
End Module
0
source

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


All Articles