HELP Please!!!!

S

Shawn

I am writing an app that has to upload .exe files into a database and return
them to folders in the app later.

I can upload and download image files just fine, but .exe's don't work. I
get an error when I try to run the downloaded file, "Is not a valid Win32
Application".

Here is my upload code...

Dim file() As Byte
Dim fs As FileStream = New FileStream("Reports.exe", FileMode.Open,
FileAccess.Read)
Dim br As BinaryReader = New BinaryReader(fs)
Dim vbFile() As Byte = br.ReadBytes(fs.Length)
br.Close()
fs.Close()

file = vbFile

cmdTwo.CommandText = "sprUploadReportApp"
cmdTwo.Parameters.Add("@ReportApp", SqlDbType.Image, file.Length).Value =
file
cmdTwo.CommandType = System.Data.CommandType.StoredProcedure
cmdTwo.ExecuteNonQuery()

And here is the download code...

cmd.Parameters.Clear()
cmd.CommandText = "Select ID, ReportApp from ReportApp"
cmd.CommandType = CommandType.Text
cmd.Connection = cnn
Dim fs As FileStream
Dim bw As BinaryWriter
Dim bufferSize As Integer = 100
Dim outbyte(bufferSize - 1) As Byte
Dim retval As Long

Dim startIndex As Long = 0
Dim id As Integer

Dim myReader As SqlClient.SqlDataReader =
cmd.ExecuteReader(CommandBehavior.SequentialAccess)


Do While myReader.Read()
id = myReader.GetValue(0)
fs = New FileStream(Path, FileMode.OpenOrCreate, FileAccess.Write)
bw = New BinaryWriter(fs)
startIndex = 0
retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize)
Do While retval = bufferSize
bw.Write(outbyte)
bw.Flush()
startIndex += bufferSize
retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize)
Loop
If retval <= 0 Then
retval = 1
End If
bw.Write(outbyte, 0, retval - 1)
bw.Flush()
bw.Close()
fs.Close()
Loop
myReader.Close()
 
?

=?ISO-8859-1?Q?G=F6ran_Andersson?=

Shawn said:
I am writing an app that has to upload .exe files into a database and return
them to folders in the app later.

I can upload and download image files just fine, but .exe's don't work. I
get an error when I try to run the downloaded file, "Is not a valid Win32
Application".

Here is my upload code...

Dim file() As Byte
Dim fs As FileStream = New FileStream("Reports.exe", FileMode.Open,
FileAccess.Read)
Dim br As BinaryReader = New BinaryReader(fs)
Dim vbFile() As Byte = br.ReadBytes(fs.Length)
br.Close()
fs.Close()

file = vbFile

cmdTwo.CommandText = "sprUploadReportApp"
cmdTwo.Parameters.Add("@ReportApp", SqlDbType.Image, file.Length).Value =
file
cmdTwo.CommandType = System.Data.CommandType.StoredProcedure
cmdTwo.ExecuteNonQuery()

And here is the download code...

cmd.Parameters.Clear()
cmd.CommandText = "Select ID, ReportApp from ReportApp"
cmd.CommandType = CommandType.Text
cmd.Connection = cnn
Dim fs As FileStream
Dim bw As BinaryWriter
Dim bufferSize As Integer = 100
Dim outbyte(bufferSize - 1) As Byte
Dim retval As Long

Dim startIndex As Long = 0
Dim id As Integer

Dim myReader As SqlClient.SqlDataReader =
cmd.ExecuteReader(CommandBehavior.SequentialAccess)


Do While myReader.Read()
id = myReader.GetValue(0)
fs = New FileStream(Path, FileMode.OpenOrCreate, FileAccess.Write)
bw = New BinaryWriter(fs)
startIndex = 0
retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize)
Do While retval = bufferSize
bw.Write(outbyte)
bw.Flush()
startIndex += bufferSize
retval = myReader.GetBytes(1, startIndex, outbyte, 0, bufferSize)
Loop
If retval <= 0 Then
retval = 1
End If
bw.Write(outbyte, 0, retval - 1)

Why are you not writing the last byte?
 
S

Shawn

Nevermind, I see what you mean. I developed that code (largely borrowed) for
another app that only uploaded pictures and I can't remember why, but I had
to drop down 1 byte, anyway, your suggestion worked great, so thanks!!!!


Shawn said:
What do you mean? What am I missing?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top