File conversions in VB...

GHoosdumGHoosdum Icrontian
edited June 2004 in Internet & Media
I'm working on something over here at work and we're encoding files into base-64. Unfortunately we can't convert back.

What our program does is this:
Allows user to browse for file (word/excel doc)
Uploads that file to the server.
Passes file to .Net COM object to convert to base-64
-> (We're in a VB 6 environment, utilizing just this one .Net COM object for the conversion - the COM object is one we created in VS.Net utilizing its conversion libraries)
virus scans base-64 file
stores base-64 file in the database

What we want to do is upon download of the file, convert it back into a readable word or excel document, and save it to the desktop. We can't find anything that gives us back our original document after it's endoced to base-64. We can convert to UTF-8, but then we get a string of gibberish when it's opened in Word.

Right now, we've taken all the middle steps out of the operation. In our test region on a desktop machine, we're simply converting a file into base-64 utilizing this COM object, then trying to convert that directly back into the original file. We can't get that to work. :rant:

Any ideas?

Comments

  • GHoosdumGHoosdum Icrontian
    edited June 2004
    Here's what we've got so far for the conversion to base-64.
    Imports System
    Imports System.IO
    'Imports System.Windows.FileDialog
    Imports System.Reflection


    <assembly:AssemblyKeyFileAttribute("TestKey.snk")>

    NameSpace B2B_components
    Public Class Base64
    ' Returns the input string encoded to base64
    Public Function ConvertToBase64(ByVal FileName as String) As String
    Dim FileDataStream As FileStream = File.Open(FileName, FileMode.Open, FileAccess.Read, FileShare.Read)
    Dim FileLength As Int32 = FileDataStream.Length
    Dim FileBytes(FileLength-1) As Byte
    FileDataStream.Read(FileBytes, 0, FileLength)
    FileDataStream.Close()
    ConvertToBase64 = Convert.ToBase64String(FileBytes)
    End Function

    '
    ' Returns the input string decoded from base64
    Public Sub ConvertFromBase64(ByVal input As String )
    Dim strBytes() As Byte = System.Convert.FromBase64String(input)
    ConvertFromBase64 = System.Text.Encoding.UTF8.GetString(strBytes)
    End sub
    End Class
    End Namespace
  • a2jfreaka2jfreak Houston, TX Member
    edited June 2004
    Try DevShed (think that's it).
  • GHoosdumGHoosdum Icrontian
    edited June 2004
    Thanks, but DevShed seems to be all for Open Source stuff, no VB help there...

    OK, now here's what's going on: instead of UTF-8, we're simply writing the byte array out to a file, using the same FileStream class we're using to pull the file out. It seems like it should work... but it doesn't. Information in the file is getting lost somewhere. I've opened the original file, and the file after decode both in Notepad, and text information, such as "EMBED Photoshop.Image.7" seems to be maintained just fine after the conversion and reconversion. However, other characters, like £ become corrupted to simpler characters, like #.

    /me scratches head...

    I'd appreciate any help, even if it's not in VB (I understand most languages) as long as it utilizes .Net it will work the same for us either way...
  • a2jfreaka2jfreak Houston, TX Member
    edited June 2004
    See if these links are helpful:

    http://www.freevbcode.com/ShowCode.asp?ID=4520
    http://www.pstruh.cz/tips/detpg_Base64.htm

    (It was just a google search, so you might have seen that already)
  • GHoosdumGHoosdum Icrontian
    edited June 2004
    Thanks!
Sign In or Register to comment.