I'm trying to maintain an old ASP page that retrieves a file from a DB and downloads it to the end user. This page works fine with older files but now "corrupts" files with a Docx file type.
I assume it was "corrupting" the older files too but Word was able to handle the minor differences whereas with the DocX format things are pickier.
The following is the code that does the download.
Set rs = Server.CreateObject("ADODB.Recordset")
' opening connection
rs.Open "SELECT FileName, FileData, ContentType FROM Docs WHERE GetDocID='" & GetDoc & "'" , conn, 2, 4
If Not rs.EOF Then
Response.Clear()
Response.AddHeader "Content-Disposition", "attachment; filename="& rs("FileName")
Response.ContentType = rs("ContentType")
Response.BinaryWrite rs("FileData")
Response.Flush()
'Response.Close()
Response.End()
End If
rs.Close
Set rs = Nothing
Any ideas what might be missing or causing the problem?
I have noticed that the pre-upload file is one byte bigger then the post-upload file.
Here is the upload code:
Dim rs
Set rs = Server.CreateObject("ADODB.Recordset")
rs.Open "Docs", conn, 2, 2
rs.AddNew
rs("FileName") = fileName
rs("FileSize") = fileSize
rs("FileData").AppendChunk = fileData
rs("ContentType") = contentType
rs("DocTitle") = DocTitle
rs("ProjectID") = ProjectID
rs.Update
rs.Close
Set rs = Nothing
Try writing explicitly in binary with the use of the ChrB(charcode) function.
(The ChrB function turns a character into a byte)
sFileData = rs("FileName")
For i=1 To Len(sFileData)
Response.BinaryWrite ChrB(Mid(sFileData,i,1))
Next
Related
i am utilizing aspSmartUpload to upload an image into an Oracle BLOB field, see below:
Set mySmartUpload = Server.CreateObject("aspSmartUpload.SmartUpload")
'*** Upload Files ***'
mySmartUpload.Upload
set file = mySmartUpload.Files("file1")
Set MyConn = Server.CreateObject("ADODB.Connection")
Call OpenConnect(MyConn)
sql = "SELECT attach_data, title, idx, attach_type_id FROM c_attachment"
Set Rs = Server.CreateObject("ADODB.recordset")
Set rs.ActiveConnection = MyConn
rs.Source = sql
rs.CursorLocation = 3
rs.CursorType = 0
rs.LockType = 3
rs.Open sql
If not file.IsMissing Then 'Check for missing file
'Add the current file in database
Rs.AddNew
file.FileToField Rs.Fields("attach_data")
Rs("title") = "title test"
rs("idx") = "134774"
rs("attach_type_id") = 1
Rs.Update
rs.Close
set rs = nothing
End If
when i check the db table. attach_data is populated. now. when i try to read the image back, i keep getting "Response object: 007~ASP 0106~Type Mismatch~An unhandled data type was encountered." error on the BinaryWrite. please help!
Set MyConn = Server.CreateObject("ADODB.Connection")
Call OpenConnect(MyConn)
SQLQuery = "select attach_data,title from c_attachment where attach_id = 109"
Set RSList = MyConn.Execute(SQLQuery)
If RSList.EOF Then
blnImgExists = False
Else
FileData = RSList("attach_data")
blnImgExists = true
End If
if blnImgExists Then
Response.Clear
Response.ContentType = "image/jpeg"
Response.AddHeader "Content-disposition", "attachment; filename=image.jpg"
Response.BinaryWrite RSList("attach_data")
else
response.write "count not open"
end if
Images should be stored as BLOB data (Binary Large Object) in Oracle, not CLOB (Character Large Object).
I am having an issue with very large CSV files not yet being created when opening them in ASP Classic. The code is below.
csvFile.close()
Set csvFile = Nothing
Set fso = Nothing
Set fso = Server.CreateObject("Scripting.FileSystemObject")
strFileFullPath = fso.BuildPath(xRefCsvRoot, filename)
Set ts = fso.OpenTextFile(strFileFullPath, 1)
strFile = ts.ReadAll
ts.Close()
response.Clear()
response.AddHeader "content-disposition", "attachment; filename=" & filename
response.Write(strFile)
response.End()
Is there an effective way to just check that the file is not blank before reading it?
Check the file size:
If fso.GetFile(strFileFullPath).Size > 0 Then
'file is not empty
End If
I am trying to read from a csv.txt file using Ado Recordset
I get no results back when trying..
When I copy the contents of the original file into a new text file, and read from that file, it works just fine.
Any ideas what the reason for this might be?
The second file is smaller in size, about 1/2. That's the only difference I can see. This is driving me mad :-)
'Edit
Update with code & schema.ini
Code:
Sub ImportTextFiles()
Dim objAdoDbConnection As ADODB.Connection
Dim objAdoDbRecordset As ADODB.Recordset
Dim strAdodbConnection As String
Dim pathSource As String
Dim filename As String
pathSource = "C:\Users\me\Desktop\Reports\"
filename = "test1.txt"
'filename = "test2.txt"
strAdodbConnection = "Provider=Microsoft.ACE.OLEDB.12.0;" _
& "Data Source=" & pathSource _
& ";Extended Properties=""text;HDR=yes;FMT=Delimited"";"
Set objAdoDbConnection = CreateObject("Adodb.Connection")
Set objAdoDbRecordset = CreateObject("ADODB.Recordset")
With objAdoDbConnection
.Open (strAdodbConnection)
With objAdoDbRecordset
.Open "Select top 10 * FROM " & filename & " WHERE [Date] > #01/01/2000# ", objAdoDbConnection, adOpenStatic, adLockOptimistic, adCmdText
If Not objAdoDbRecordset.EOF Then objAdoDbRecordset.MoveFirst
Do While Not objAdoDbRecordset.EOF
Debug.Print "Field(0): " & objAdoDbRecordset(0).Value
objAdoDbRecordset.MoveNext
Loop
.Close
End With
.Close
End With
Set objAdoDbRecordset = Nothing
Set objAdoDbConnection = Nothing
End Sub
Schema.ini:
[Test1.txt]
col1=date text
col2=interval integer
col3=application text
[Test2.txt]
col1=date text
col2=interval integer
col3=application text
notepadd++ gave me the answer, file1 is ucs-2 encoded, the newly created utf-8
I need source code for reading the .txt content from a URL.
My text file content sample and then load in Visual Basic 6.0:
My source code:
Dim data As String
data = Inet1.OpenURL("http://test.com/sample.txt")
Text1.Text = data
There is nothing that will only "download" a line at a time as it can't tell where the line breaks are until it's downloaded it.
If you only want to read/process a line at a time, you can split on the line breaks after downloading it:
Dim Data As String
Dim DataLines() As String
Data = Inet1.OpenURL("http://test.com/sample.txt")
DataLines = Split(Data, vbCrLf)
For Index = LBound(DataLines) to UBound(DataLines)
MsgBox DataLines(Index)
Next
You will need to be careful to make sure you have the correct line break for the data being read.
When dealing with HTTP you have to consider both line separators and character encoding. If you can make assumptions after testing then you can bypass some checking and just hard-code to fit your needs.
However the creay old Internet Transfer Control ("Inet") is usually not the best choice available and more modern alternatives are shipped as part of Windows since at least the advent of IE 5.5, and installed with IE 5.5 on more ancient versions of Windows. Thus they'll even be available and work on nearly any Win95 system still running today.
'References to MSXML 3.0 or later,
' ADO 2.5 or later.
Private Function GetHttpText(ByVal URL As String) As ADODB.Stream
Dim Req As MSXML2.XMLHTTP
Dim CharSet As String
Dim CharsetPos As Long
Dim LineSeparator As LineSeparatorEnum
Set Req = New MSXML2.XMLHTTP
Set GetHttpText = New ADODB.Stream
With GetHttpText
.Open
.Type = adTypeBinary
With Req
.Open "GET", URL, False
.send
CharSet = LCase$(.getResponseHeader("CONTENT-TYPE"))
End With
.Write Req.responseBody
CharsetPos = InStr(CharSet, "charset")
If CharsetPos Then
CharSet = Split(Mid$(CharSet, CharsetPos), "=")(1)
Else
'UTF-8 is a reasonable "default" these days:
CharSet = "utf-8"
End If
If CharSet = "utf-8" Then
LineSeparator = adLF
Else
'Your milage may vary here, since there is no line-end
'header defined for HTTP:
LineSeparator = adCRLF
End If
.Position = 0
.Type = adTypeText
.CharSet = CharSet
.LineSeparator = LineSeparator
End With
End Function
Private Sub DumpTextLineByLine()
With GetHttpText("http://textfiles.com/art/simpsons.txt")
'Read text line by line to populate a multiline TextBox
'just as a demonstration:
Do Until .EOS
Text1.SelText = .ReadText(adReadLine)
Text1.SelText = vbNewLine
Loop
.Close
End With
End Sub
i have a vbscript which connects to db2 and recset gets long varchar 18000 (contains xml message).
The problem is that variable in vbscript has length only 250.
Ok, i have divided recset to array(50) 250 chars each string.
Then when i trying to pass first string from array to file it throws error.
Because in array(0) string there are a lot of quotes. How can i save result to file?
sql = "select message_data from messages where MESSAGE_ID = '5461654648464'"
Set objConnection = CreateObject("ADODB.Connection")
objConnection.ConnectionString = "Provider=ibmdadb2; DSN=TEST; UID=user; PWD=password"
objConnection.Open
Set recset = CreateObject("ADODB.Recordset")
recset.Open sql,objConnection
if recset.EOF then WScript.Echo "No found" else splt recset("message_data") end if
recset.Close
objConnection.Close
function splt (strg)
dim arr(50)
Set fso = CreateObject("Scripting.FileSystemObject")
sFolder = "C:\jdk1.3\temp\arch"
Set NewFile = fso.CreateTextFile(sFolder&"\file.txt", True)
if len(strg) > 250 then ll = round(len(strg)/250, 0) + 1
for i = 0 to ll
arr(i) = left(right(strg, abs(Cint(len(strg))-250*i)), 250)
txt = arr(i)
NewFile.Write txt
next
NewFile.Close
End function
#Ruslan: Make sure the file exists first (it can be just a blank text file) and I'd suggest you also update your function with
Dim arr(50), fso, sFolder, NewFile, ll, txt, i
and add Option Explicit right at the top of the file as well.