473,796 Members | 2,538 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Decoding Base64 and Saving to a file - HELP!

Hey Peeps,

Ok here is my situation.. I have a Java applet which allows the user to select files and upload them to the server. The
applet converts the file to Base64 and then POSTS the data to an ASP page.

The ASP code I have is:

<%
Base64Chars = "ABCDEFGHIJKLMN OPQRSTUVWXYZabc defghijklmnopqr stuvwxyz0123456 789+/"

Public Function Base64Decode(sB ase64)
For i = 1 To Len(sBase64) Step 4
w1 = FindPos(Mid(sBa se64, i, 1))
w2 = FindPos(Mid(sBa se64, i + 1, 1))
w3 = FindPos(Mid(sBa se64, i + 2, 1))
w4 = FindPos(Mid(sBa se64, i + 3, 1))
If (w2 >= 0) Then ByteArray = ByteArray & chrB((w1 * 4 + Int(w2 / 16)) And 255)
If (w3 >= 0) Then ByteArray = ByteArray & chrB((w2 * 16 + Int(w3 / 4)) And 255)
If (w4 >= 0) Then ByteArray = ByteArray & chrB((w3 * 64 + w4) And 255)
Next
Base64Decode = ByteArray
End Function

Private Function FindPos(sChar)
If (Len(sChar) = 0) Then
FindPos = -1
Else
FindPos = InStr(Base64Cha rs, sChar) - 1
End If
End Function
Set oStream = Server.CreateOb ject("ADODB.Str eam")
oStream.Type = 1
oStream.Open
oStream.Write Base64Decode(Re quest.Form("fil e"))
oStream .SaveToFile "c:\file.da t"
oStream.Close()
Set oStream = Nothing
%>

I am receving the following error:

ADODB.Stream error '800a0bb9'

Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.

/upload/upload.asp, line 29

Any ideas?? This is all new to me.. any help would be much appreciated!!

Thanks in advance!

AJB

Aug 16 '05 #1
2 8336
<pl***@plank.co m> wrote in message
news:ui******** *************** *********@4ax.c om...
Hey Peeps,

Ok here is my situation.. I have a Java applet which allows the user to select files and upload them to the server. The applet converts the file to Base64 and then POSTS the data to an ASP page.

The ASP code I have is:

<%
Base64Chars = "ABCDEFGHIJKLMN OPQRSTUVWXYZabc defghijklmnopqr stuvwxyz0123456 789+/"
Public Function Base64Decode(sB ase64)
For i = 1 To Len(sBase64) Step 4
w1 = FindPos(Mid(sBa se64, i, 1))
w2 = FindPos(Mid(sBa se64, i + 1, 1))
w3 = FindPos(Mid(sBa se64, i + 2, 1))
w4 = FindPos(Mid(sBa se64, i + 3, 1))
If (w2 >= 0) Then ByteArray = ByteArray & chrB((w1 * 4 + Int(w2 / 16)) And 255) If (w3 >= 0) Then ByteArray = ByteArray & chrB((w2 * 16 + Int(w3 / 4)) And 255) If (w4 >= 0) Then ByteArray = ByteArray & chrB((w3 * 64 + w4) And 255)
Next
Base64Decode = ByteArray
End Function

Private Function FindPos(sChar)
If (Len(sChar) = 0) Then
FindPos = -1
Else
FindPos = InStr(Base64Cha rs, sChar) - 1
End If
End Function
Set oStream = Server.CreateOb ject("ADODB.Str eam")
oStream.Type = 1
oStream.Open
oStream.Write Base64Decode(Re quest.Form("fil e"))
oStream .SaveToFile "c:\file.da t"
oStream.Close()
Set oStream = Nothing
%>

I am receving the following error:

ADODB.Stream error '800a0bb9'

Arguments are of the wrong type, are out of acceptable range, or are in conflict with one another.
/upload/upload.asp, line 29

Any ideas?? This is all new to me.. any help would be much appreciated!!

Thanks in advance!

AJB


Try it without oStream to see what you get:

WScript.Echo Base64Decode(Re quest.Form("fil e"))

Here's an alternative:

Encode and Decode Base64 Files
http://www.fourmilab.ch/webtools/base64/

Option Explicit
Dim objWSS
Set objWSS = WScript.CreateO bject("WScript. Shell")
objWSS.Run "%comspec% /C base64.exe -d encoded.txt decoded.txt", 1, True
Set objWSS = Nothing
Aug 16 '05 #2

<pl***@plank.co m> wrote in message
news:ui******** *************** *********@4ax.c om...
Hey Peeps,

Ok here is my situation.. I have a Java applet which allows the user to
select files and upload them to the server. The
applet converts the file to Base64 and then POSTS the data to an ASP page.

The ASP code I have is:

<%
Base64Chars =
"ABCDEFGHIJKLMN OPQRSTUVWXYZabc defghijklmnopqr stuvwxyz0123456 789+/"

Public Function Base64Decode(sB ase64)
For i = 1 To Len(sBase64) Step 4
w1 = FindPos(Mid(sBa se64, i, 1))
w2 = FindPos(Mid(sBa se64, i + 1, 1))
w3 = FindPos(Mid(sBa se64, i + 2, 1))
w4 = FindPos(Mid(sBa se64, i + 3, 1))
If (w2 >= 0) Then ByteArray = ByteArray & chrB((w1 * 4 + Int(w2 / 16)) And
255)
If (w3 >= 0) Then ByteArray = ByteArray & chrB((w2 * 16 + Int(w3 / 4)) And
255)
If (w4 >= 0) Then ByteArray = ByteArray & chrB((w3 * 64 + w4) And 255)
Next
Base64Decode = ByteArray
End Function

Private Function FindPos(sChar)
If (Len(sChar) = 0) Then
FindPos = -1
Else
FindPos = InStr(Base64Cha rs, sChar) - 1
End If
End Function
Set oStream = Server.CreateOb ject("ADODB.Str eam")
oStream.Type = 1
oStream.Open
oStream.Write Base64Decode(Re quest.Form("fil e"))
oStream .SaveToFile "c:\file.da t"
oStream.Close()
Set oStream = Nothing
%>

I am receving the following error:

ADODB.Stream error '800a0bb9'

Arguments are of the wrong type, are out of acceptable range, or are in
conflict with one another.

/upload/upload.asp, line 29

Any ideas?? This is all new to me.. any help would be much appreciated!!
Put this code in a COM object in VB6.
Compile with 'ignore integer overflow'.

Private Enum baseFlag
ATL_BASE64_FLAG _NONE = 0
ATL_BASE64_FLAG _NOPAD
ATL_BASE64_FLAG _NOCRLF
End Enum

' based on atlenc.h
' Warning! You cannot run this function in debug mode
' it only works in compiled mode
' we got stunning speed in VB6, in compare with the
' 'other' sucking Chr() and Asc() routines which might be 100x slower than
' this version. The drawback is that we must compile with
' -ignore integer overflow- which is not a drawback for me anyway.
PUblic Function btFromBase64(By Ref sBaseEncodedBLO B As String) As Byte()
'// walk the source buffer
'// each four character sequence is converted to 3 bytes
'// CRLFs and =, and any characters not in the encoding table
'// are skiped

Dim dx As Long
Dim cx As Long
Dim dwCurr As Long
Dim i As Long
Dim nSrcLen As Long

nSrcLen = LenB(sBaseEncod edBLOB)
If nSrcLen = 0 Then Exit Function

Dim nBits As Long
Dim nch As Byte
Dim pbDest() As Byte
' copy our string to a fast loopable byte array
' this array is also our target array
' it is possible because the read pointer goes 'faster' than the write
pointer
pbDest() = sBaseEncodedBLO B
' no need for this memory waste anymore
sBaseEncodedBLO B = vbNullString

Do
dwCurr = 0
nBits = 0
For i = 3 To 0 Step -1
If cx >= nSrcLen Then Exit For
nch = pbDest(cx)
cx = cx + 2
If nch >= 65 And nch <= 90 Then
nch = nch - 65 ' // 0 range starts at 'A'
ElseIf nch >= 97 And nch <= 122 Then
nch = nch - 71 '(97 - 26) ' - 'a' + 26, Byte) ' // 26
range starts at 'a'
ElseIf nch >= 48 And nch <= 57 Then
nch = nch + 4 '- '0' + 52; // 52 range starts at '0'
ElseIf nch = 43 Then
nch = 62
ElseIf nch = 47 Then
nch = 63
Else
nch = &HFF
'// skip this char
i = i + 1
End If
If nch <> &HFF Then
dwCurr = dwCurr * &H40 Or nch
nBits = nBits + 6
End If
Next
'// dwCurr has the 3 bytes to write to the output buffer
'// left to right
dwCurr = dwCurr * IntPower(24 - nBits)
For i = nBits \ 8 - 1 To 0 Step -1
pbDest(dx) = (dwCurr And &HFF0000) \ &H10000
dx = dx + 1
dwCurr = dwCurr * &H100
Next
Loop While cx < nSrcLen
ReDim Preserve pbDest(dx - 1)
btFromBase64 = pbDest

End Function

' this function is much better than using the ^ operator
' since that operator is using slow floating point code ops
Private Function IntPower(ByVal pwr As Long) As Long

IntPower = 1

For pwr = 1 To pwr
IntPower = IntPower * 2
Next

End Function

Public Function Base64EncodeGet RequiredLength( ByVal nSrcLen As Integer,
Optional ByVal dwFlags As baseFlag = baseFlag.ATL_BA SE64_FLAG_NONE) As Long

Dim nRet As Long
nRet = (nSrcLen * 4) \ 3

If ((dwFlags And baseFlag.ATL_BA SE64_FLAG_NOPAD ) = 0) Then
nRet = nRet + nSrcLen Mod 3
End If

Dim nCRLFs As Long
nCRLFs = nRet \ 76 + 1
Dim nOnLastLine As Long
nOnLastLine = nRet Mod 76

If (nOnLastLine > 0) Then

If (nOnLastLine Mod 4) > 0 Then
nRet = nRet + 4 - (nOnLastLine Mod 4)
End If
End If

nCRLFs = nCRLFs * 2

If ((dwFlags And baseFlag.ATL_BA SE64_FLAG_NOCRL F) = 0) Then
nRet = nRet + nCRLFs
End If

Base64EncodeGet RequiredLength = nRet
End Function

Public Function ByteToBase64Arr ay(ByRef pbSrcData() As Byte, ByRef szDest()
As Byte, ByVal nSrcLen As Long, Optional ByVal dwFlags As baseFlag =
baseFlag.ATL_BA SE64_FLAG_NONE) As Long
Static base(63) As Byte
Dim cx As Long, x As Long
For x = 65 To 90
base(cx) = x
cx = cx + 1
Next
For x = 97 To 122
base(cx) = x
cx = cx + 1
Next
For x = 48 To 57
base(cx) = x
cx = cx + 1
Next
base(cx) = 43
cx = cx + 1
base(cx) = 47

Debug.Assert (UBound(szDest) >= Base64EncodeGet RequiredLength( nSrcLen,
dwFlags))
cx = 0
Dim nLen1 As Long
nLen1 = (nSrcLen \ 3) * 4
Dim nLen2 As Long
nLen2 = nLen1 \ 76
Dim nLen3 As Long
nLen3 = 19
Dim i As Long

Dim dx As Long
'Dim b As Long
Dim dwCurr As Long

Dim k As Long
Dim n As Long
Dim j As Long
Dim nDestLen As Long
nDestLen = UBound(szDest)
' for each line of 76 chars
For i = 0 To nLen2
If i = nLen2 Then nLen3 = (nLen1 Mod 76) \ 4

'for this line
For j = 0 To nLen3 - 1
dwCurr = 0
For n = 2 To 0 Step -1
dwCurr = dwCurr Or pbSrcData(cx)
cx = cx + 1
dwCurr = dwCurr * &H100
Next
For k = 3 To 0 Step -1
szDest(dx) = base((dwCurr \ &H4000000) And &H3F)
dx = dx + 1
dwCurr = dwCurr * &H40
Next
Next
If (dwFlags And baseFlag.ATL_BA SE64_FLAG_NOCRL F) = 0 Then
szDest(dx) = 13
dx = dx + 1 ' '\r';
szDest(dx) = 10
dx = dx + 1
End If
Next

If (dx > 0 And (dwFlags And baseFlag.ATL_BA SE64_FLAG_NOCRL F) = 0) Then
dx = dx - 2
End If
Dim nPad As Long
nPad = nSrcLen Mod 3
If nLen2 <> nPad Then
nLen2 = nPad + 1
Else
nLen2 = 0
End If

If nLen2 > 0 Then
dwCurr = 0
For n = 0 To 2
If n < nPad Then
dwCurr = dwCurr Or pbSrcData(cx)
cx = cx + 1
End If
dwCurr = dwCurr * &H100
Next

For k = nLen2 - 1 To 0 Step -1
'&H3F repairs negative signed longs
szDest(dx) = base((dwCurr \ &H4000000) And &H3F)
dx = dx + 1
dwCurr = dwCurr * &H40
Next
If (dwFlags And baseFlag.ATL_BA SE64_FLAG_NOPAD ) = 0 Then
If nLen3 <> nLen2 Then
nLen3 = 4 - nLen2
Else
nLen3 = 0
End If

For j = nLen3 - 1 To 0 Step -1
If dx >= nDestLen Then Exit For
szDest(dx) = 61 ''=';
dx = dx + 1
Next
End If
End If
ByteToBase64Arr ay = dx

End Function
Thanks in advance!

AJB


Aug 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

27
15055
by: gRizwan | last post by:
Hello all, We have a problem on a webpage. That page is sent some email data in base64 format. what we need to do is, decode the base64 data back to original shape and extract attached image from it. Any help will be highly appriciated. Thanks
14
4357
by: BB | last post by:
Hello. i am trying to decode a block of (what i think is) base64 into text, and i have NO idea how to begin. I'm going to paste the whole string here, but i want to know the steps necessary to convert it to text. i've tried the usual binary/ascii techniques, but they just seem to loop back in to each other. what is the step by step to get from base64 to text? this is for an evil riddle i'm trying to solve. thanks. Becca. here's the...
1
6860
by: maxxxxel | last post by:
Hi Can anyone help me with some asp code , I changed the code to use CDO.message instead of the old cdont.sys to send mail from a ASP webpage which works fine. Our problem is that when we send mail externally to a internet email site like Gmail the PDF is sent but is corrupted because CDOSYS ends up using binary encoding rather than Base64 encoding when creating the attachment. More information here: Anthonys Code My knowledge of ASP...
5
6433
by: JohnLorac | last post by:
Hello, can somebody help me with saving file into local disk using javascript? I made some sample code which unfortunately won't work :(. Applet sample file: public class IO extends Applet {
0
9684
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9530
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10459
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10236
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9055
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7552
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6793
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5577
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2928
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.