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.
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)
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 ????
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 ?
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.
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 ???
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
MSDN Community Support
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
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!
Member
23 Points
128 Posts
Problem with Converting UBASE64 string to a File
Jan 02, 2018 06:04 PM|shehz81|LINK
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)
All-Star
48320 Points
18012 Posts
Re: Problem with Converting UBASE64 string to a File
Jan 02, 2018 06:51 PM|PatriceSc|LINK
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 ????
Member
23 Points
128 Posts
Re: Problem with Converting UBASE64 string to a File
Jan 02, 2018 07:04 PM|shehz81|LINK
sorry I added more information. thanks
All-Star
48320 Points
18012 Posts
Re: Problem with Converting UBASE64 string to a File
Jan 02, 2018 07:24 PM|PatriceSc|LINK
Ah, and you tried previously with the utf8 object which is not used at all for now? It would give something such as (untested) :
Does it look fine if opended with notepad ? It could be also a wrong encoding option when you import this file into Excel ?
Member
23 Points
128 Posts
Re: Problem with Converting UBASE64 string to a File
Jan 02, 2018 08:52 PM|shehz81|LINK
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!
All-Star
48320 Points
18012 Posts
Re: Problem with Converting UBASE64 string to a File
Jan 02, 2018 09:05 PM|PatriceSc|LINK
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 ???
Contributor
6730 Points
2715 Posts
Re: Problem with Converting UBASE64 string to a File
Jan 03, 2018 03:53 AM|Eric Du|LINK
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:
Best Regards,
Eric Du
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.
Member
23 Points
128 Posts
Re: Problem with Converting UBASE64 string to a File
Jan 03, 2018 02:46 PM|shehz81|LINK
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!