# Problem with Converting UBASE64 string to a File

• ### Question

• User-1158855013 posted

I"m recieving a binary data and extension from an xml file. I convert it to bytes as shown below,  but when I open it up in excel , the text is all gibberish.

if I go to this site https://www.freeformatter.com/base64-encoder.html and enter the same string and decode it, I can save and read the file correctly.

Something must be wrong in the code that is not properly converting the data to a file. Please advise.

Dim filename As String = Request.Form("filename")
Dim filebytes As String = Request.Form("filebytes")
Dim fileext As String = Request.Form("fileext")

Dim utf8 As Encoding = Encoding.UTF8()
Dim bytes As Byte() = Convert.FromBase64String(base64String)
       Dim filePath As String = Server.MapPath("~/App_Data/" + filename)
        File.WriteAllBytes(filePath, bytes)

Tuesday, January 2, 2018 6:04 PM

• User-1838255255 posted

Hi shehz81,

According to your description, I am not clear which format is you wanted? i write encode and decode methods to convert data in txt format, please check the following sample code:

Sample Code:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim plte As String = "Read Value"
Dim data As String = Base64Encode(plte)
Dim newdata As String = Base64Decode(data)
System.IO.File.WriteAllText("D:\text.txt", newdata)
End Sub

Public Shared Function Base64Encode(ByVal plainText As String) As String
Dim plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText)
Return System.Convert.ToBase64String(plainTextBytes)
End Function

Public Shared Function Base64Decode(ByVal base64EncodedData As String) As String
Dim base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData)
Return System.Text.Encoding.UTF8.GetString(base64EncodedBytes)
End Function

Best Regards,

Eric Du

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Wednesday, January 3, 2018 3:53 AM

### All replies

• User753101303 posted

Hi,

Please be always explicit about what happens rather than telling things such as "not correct" or "doesn't work".

For example my first though is that the FromBase64String call fails because of a data url prefix as shown at https://en.wikipedia.org/wiki/Data_URI_scheme#Syntax ? If yes you could use this prefix to check this is the kind of data you expect but it should be skipped for the conversion as it is not really part of the actual base64 data.

If you meant it works but the content doesn't seems what you expect do you have a sample file you could test ? Usually I save then what I received on the server side and I can compare the length and even the content of the file to better understand the exact difference I find and possibly its root cause.

For now my guess is that the FromBase64String call fails ????

Tuesday, January 2, 2018 6:51 PM
• User-1158855013 posted

Tuesday, January 2, 2018 7:04 PM
• User753101303 posted

Ah, and you tried previously with the utf8 object which is not used at all for now? It would give something such as (untested) :

Dim filename As String = Request.Form("filename")
Dim filebytes As String = Request.Form("filebytes")
Dim fileext As String = Request.Form("fileext")

Dim utf8 As Encoding = Encoding.UTF8()
Dim bytes As Byte() = Convert.FromBase64String(base64String)
Dim filePath As String = Server.MapPath("~/App_Data/" + filename)
File.WriteAllText(filePath, utf8.GetString(bytes))

Does it look fine if opended with notepad ? It could be also a wrong encoding option when you import this file into Excel ?

Tuesday, January 2, 2018 7:24 PM
• User-1158855013 posted

Hi

I just tried the way you suggested and it is still giving me all giberrish text in excel file. It does create an excel file without any issue though. Not sure how else would I process the binary data.

Also I noticed when I paste the string on this site https://www.freeformatter.com/base64-encoder.html

to decode, it saves with ".data" extension. I have to open the file from excel to read it correctly.

Thanks!

Tuesday, January 2, 2018 8:52 PM
• User753101303 posted

And this is posted to the server using a form on which you have control ? Do you know how it is processed before before being put inside the filebytes input field ? Could it be System.Text.Unicode rather than System.Text.Utf8 ?

You really don"t know what is the format of the source data ?

Edit filebytes is not an input type=file control ???

<sub></sub><sup></sup>

Tuesday, January 2, 2018 9:05 PM
• User-1838255255 posted

Hi shehz81,

According to your description, I am not clear which format is you wanted? i write encode and decode methods to convert data in txt format, please check the following sample code:

Sample Code:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim plte As String = "Read Value"
Dim data As String = Base64Encode(plte)
Dim newdata As String = Base64Decode(data)
System.IO.File.WriteAllText("D:\text.txt", newdata)
End Sub

Public Shared Function Base64Encode(ByVal plainText As String) As String
Dim plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText)
Return System.Convert.ToBase64String(plainTextBytes)
End Function

Public Shared Function Base64Decode(ByVal base64EncodedData As String) As String
Dim base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData)
Return System.Text.Encoding.UTF8.GetString(base64EncodedBytes)
End Function

Best Regards,

Eric Du

• Marked as answer by Thursday, October 7, 2021 12:00 AM
Wednesday, January 3, 2018 3:53 AM
• User-1158855013 posted

I realized when I was posting the binary data from one page to another, it converted all "+" signs to an empty space which corrupted the data. Thank you all for the help!

Wednesday, January 3, 2018 2:46 PM