Page 1 of 1

ASP(VB).NET character output

Posted: Fri May 23, 2008 10:06 pm
by bitWISE
What I need to do is send data out through the browser in a downloadable text file. The client sends this file to a corporate printing company that parses the file out onto their premade forms. The file is tab delimited and the content contains newline characters. To preserve the newlines so that the line only breaks at the end and not in the middle of content, I need to replace them with the hex character "ff" which is supposed to be Chr(255) in VB. However, no matter what I try the end result always gives me "c3 bf".

Here is the relevant code.

Code: Select all

Public Sub Format_Export_Field(ByVal pval As String, ByRef pstr As System.Text.StringBuilder)
		pval = Replace(pval, ControlChars.Back, String.Empty)
		pval = Replace(pval, ControlChars.Cr, String.Empty)
		pval = Replace(pval, ControlChars.Lf, String.Empty)
		pval = Replace(pval, ControlChars.FormFeed, String.Empty)
		pval = Replace(pval, ControlChars.Tab, String.Empty)
		pval = Trim(pval)

		pstr.Append(pval & ControlChars.Tab)
	End Sub

Public Sub Generate_TXT_Renewal_Notice(ByVal batchID As String, ByVal renewalID As Integer, ByVal psumall As Integer)
        Dim ds As System.Data.DataSet
		Dim dv As System.Data.DataView
        Dim output As StringBuilder
        Dim clsRenewals As New TspEstimate.Renewal(m_user)

		ds = clsRenewals.Get_Renewals_By_Batch(batchID, renewalID, psumall)

        If ds.Tables.Count = 0 Then
            Exit Sub
        End If

        Response.ClearContent()
        Response.ContentType = "text/plain"
        Response.Charset = "UTF-8"
        Response.AppendHeader("content-disposition", "attachment; filename=" & _
            "RenewalExport_" & Now.Year & Now.Month & Now.Day & ".txt")

        dv = New System.Data.DataView(ds.Tables(0))
        For Each dvr As Data.DataRowView In dv
            output = New StringBuilder

            '1
            Format_Export_Field(dvr("branchname"), output)
            '2
            Format_Export_Field(LTrim(dvr("sourcestreet") & " ") & dvr("sourcestreet2"), output)
            '3
            Format_Export_Field(dvr("sourcecity"), output)
            '4
            Format_Export_Field(dvr("sourcephone"), output)
            '5
            Format_Export_Field(dvr("targetbusinessname"), output)
            '6
            Format_Export_Field(dvr("targetname"), output)
            '7
            Format_Export_Field(LTrim(dvr("targetstreet") & " ") & dvr("targetstreet2"), output)
            '8
            Format_Export_Field(dvr("target_city"), output)
            '9
            Format_Export_Field(dvr("targetstate"), output)
            '10
            Format_Export_Field(dvr("targetpostal"), output)
            '11
            Format_Export_Field(dvr("targetpostalex"), output)
            '12
            Format_Export_Field(dvr("servicebusinessname"), output)
            '13
            Format_Export_Field(dvr("servicename"), output)
            '14
            Format_Export_Field(LTrim(dvr("servicesitestreet") & " ") & dvr("servicesitestreet2"), output)
            '15
            Format_Export_Field(dvr("servicesitecity"), output)
            '16
            If dvr("renewaltext") Is System.DBNull.Value Then
                output.Append(Chr(255), 4)
                output.Append(Replace(dvr("renewaltext2"), ControlChars.NewLine, Chr(255)) & ControlChars.Tab)
            Else
                output.Append(Chr(255), 4)
                output.Append(Replace(dvr("renewaltext"), ControlChars.NewLine, Chr(255)) & ControlChars.Tab)
            End If
            '17
            Format_Export_Field(dvr("renewalamount"), output)
            '18
            Format_Export_Field(dvr("renewaltax"), output)
            '19
            Format_Export_Field(dvr("renewalamount") + dvr("renewaltax"), output)
            '20
            Format_Export_Field(dvr("programname"), output)
            '21
            Format_Export_Field(Month(dvr("renewaldate")) & "/" & Day(dvr("warrantydate")) & "/" & Year(dvr("renewaldate")), output)
            '22
            Format_Export_Field(dvr("accountnum"), output)
            '23
            Format_Export_Field(dvr("programid"), output)

            output.Append(ControlChars.NewLine)
            Response.Write(output.ToString)
        Next

        Response.Flush()
        Response.Close()
	End Sub

Re: ASP(VB).NET character output

Posted: Mon May 26, 2008 11:34 am
by ^misantropia^
You are sending it to the browser as UTF-8. UTF-8 is a run-length encoding that uses the high bit of a byte to signal that a character sequence is coming up. 0xFF has that high bit set but no legible sequence following it, so it confuses the browser something awful.

Solution: send your response as plain ASCII or with content type application/x-octet-stream. The latter is arguably the better solution but has the drawback that the browser won't treat the response as plain text anymore (obviously), but will instead offer to download and save the file.

Re: ASP(VB).NET character output

Posted: Tue May 27, 2008 12:42 pm
by bitWISE
Alright I'll give that a shot. I knew it had to do with my type/charset but I just couldn't find a winner and I didn't want to deal with intermediate files.

Re: ASP(VB).NET character output

Posted: Tue May 27, 2008 1:51 pm
by bitWISE
No luck. I can use notepad++ to convert the file to ANSI and then it shows up as ff but neither of those ideas worked.

Re: ASP(VB).NET character output

Posted: Tue May 27, 2008 2:57 pm
by bitWISE
Should anyone need to know the answer:

Response.ContentType = "text/plain"
Response.ContentEncoding = Encoding.GetEncoding("Windows-1252")
Response.Charset = "Windows-1252"