Home > Archive > ASP > February 2006 > retrieving document from SQL 2000 problem
You are viewing an archived Text-only version of the thread.
To view this thread in it's original format and/or if you want to reply to
this thread please [click here]
| Author |
retrieving document from SQL 2000 problem
|
|
| znubbe@hotmail.com 2006-02-17, 6:55 pm |
| Hi,
I hope anyone can help me with this problem.
I have a field of image type in a SQL 2000 database. I'm using this
code to insert a document:
Dim conn
Dim rs
Dim oStream
Session.Codepage =3D 65001
Response.Charset =3D "utf-8"
response.clear
response.expires =3D 0
response.buffer =3D true
'Response.ContentType =3D "application/pdf"
Response.ContentType =3D "application/ms-word"
Set conn =3D CreateObject("ADODB.Connection")
conn.Open "Provider=3DSQLOLEDB.1;Integrated Security=3DSSPI;Persist
Security Info=3DFalse;Initial Catalog=3DPBase;Data Source=3DBURKEN"
strSQL =3D "Select * From News where NEWS_ID=3D1"
Set oRS =3D CreateObject("ADODB.Recordset")
Call oRS.Open(strSQL, conn, 2, 2)
Set oStream =3D CreateObject("ADODB.Stream")
oStream.Type =3D 1
oStream.Open
oStream.LoadFromFile " C:\Inetpub\wwwroot\PB_Kansli\Files\lucul
li.doc"
oRS.Fields("NEWS_DATA").Value =3D oStream.Read
oStream.Close
Set oStream =3Dnothing
oRS.Update
oRS.Close
Set oRS =3Dnothing
conn.Close
Set conn =3D nothing
Data is put in the field and I'm using this code to retrieve it:
Set conn =3D CreateObject("ADODB.Connection")
conn.Open "Provider=3DSQLOLEDB.1;Integrated Security=3DSSPI;Persist
Security Info=3DFalse;Initial Catalog=3DPBase;Data Source=3DBURKEN"
strSQL =3D "Select * From News where NEWS_ID=3D1"
Set oRS =3D CreateObject("ADODB.Recordset")
Call oRS.Open(strSQL, conn, 2, 2)
Response.AddHeader "Content-Disposition", "inline;filename=3Dtest.doc"
Response.BinaryWrite oRS("NEWS_DATA")
oRS.Close
Set oRS =3Dnothing
conn.Close
Set conn =3D nothing
The problem is that the output is nothing like the orginal. Instead of
a word document I get this:
=D0=8F=E0=A1=B1=E1=80=80>?ku?jx=E6=80=80?????????????????????????????=
???????????????????????????????????????????????????????????????????????????=
??=EC=A5=813
??bjbjC?C?=E2=83=80!=ED=80=80!=ED=80=80v,???l\\\\????vv=
v8? ??T
..=E2=85=80=E2=85=80=E2=85=80=E2=85=80?Ig s=D3=88=D5=88=D5=88=D5=88=
=D5=88=D5=88=D5=88$? ?
???{''"{{??\\=E2=85=80? ???{\=E2=85=80\8=E2=85=80=D3=88?{=
=D3=88??w\?w=E2=85=80=D6=85
=E0=B3=8C,=CC=B3=C6=81??v{Xww\$ 0T w* =D3=87(* w???\\\\
etc etc etc etc etc
What am I doing wrong?
Please help, I'm going crazy over this :-(
Thanks for your help,
Znubbe
| |
| Bob Barrows [MVP] 2006-02-17, 6:55 pm |
| znubbe@hotmail.com wrote:
> Hi,
>
> I hope anyone can help me with this problem.
>
> I have a field of image type in a SQL 2000 database. I'm using this
> code to insert a document:
>
> Dim conn
> Dim rs
> Dim oStream
>
> Session.Codepage = 65001
> Response.Charset = "utf-8"
>
> response.clear
> response.expires = 0
> response.buffer = true
>
> 'Response.ContentType = "application/pdf"
> Response.ContentType = "application/ms-word"
>
> Set conn = CreateObject("ADODB.Connection")
> conn.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist
> Security Info=False;Initial Catalog=PBase;Data Source=BURKEN"
>
> strSQL = "Select * From News where NEWS_ID=1"
> Set oRS = CreateObject("ADODB.Recordset")
> Call oRS.Open(strSQL, conn, 2, 2)
>
> Set oStream = CreateObject("ADODB.Stream")
> oStream.Type = 1
> oStream.Open
> oStream.LoadFromFile " C:\Inetpub\wwwroot\PB_Kansli\Files\lucul
li.doc"
> oRS.Fields("NEWS_DATA").Value = oStream.Read
> oStream.Close
> Set oStream =nothing
> oRS.Update
> oRS.Close
> Set oRS =nothing
>
> conn.Close
> Set conn = nothing
>
>
> Data is put in the field and I'm using this code to retrieve it:
>
> Set conn = CreateObject("ADODB.Connection")
> conn.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist
> Security Info=False;Initial Catalog=PBase;Data Source=BURKEN"
>
> strSQL = "Select * From News where NEWS_ID=1"
Why retrieve all the columns? It's usually a good idea to specify the column
you wish to retrieve...
> Set oRS = CreateObject("ADODB.Recordset")
> Call oRS.Open(strSQL, conn, 2, 2)
>
> Response.AddHeader "Content-Disposition", "inline;filename=test.doc"
> Response.BinaryWrite oRS("NEWS_DATA")
> oRS.Close
> Set oRS =nothing
>
> conn.Close
> Set conn = nothing
>
I think you need to use a Stream object. Try this:
If Not ors.eof then
Set FStream = Server.CreateObject("ADODB.Stream")
FStream.Type = adTypeBinary
FStream.Open
FStream.write rs("NEWS_DATA").Value
FStream.Position = 0
Response.AddHeader "Content-Length", FStream.size
Response.Charset = "UTF-8"
Response.BinaryWrite FStream.Read
Response.flush
end if
HTH,
Bob Barrows
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
| |
| Dave Anderson 2006-02-17, 6:55 pm |
| znubbe@hotmail.com wrote:
> Set oStream = CreateObject("ADODB.Stream")
> oStream.Type = 1
> oStream.Open
> oStream.LoadFromFile " C:\Inetpub\wwwroot\PB_Kansli\Files\lucul
li.doc"
> oRS.Fields("NEWS_DATA").Value = oStream.Read
I have not done it with this with a recordset, but I have done it with a
stored procedure. And in that case, it was necessary to set the value AND
size of the parameter. Consider this example:
CREATE PROCEDURE dbo.News_AttachDocument (
@ID INT,
@Data IMAGE,
@ContentType VARCHAR(50)
) AS
UPDATE News
SET NEWS_DATA = @Data,
CONTENT_TYPE = @ContentType
WHERE NEWS_ID = @ID
GO
To insert the data, use something like this. I will attempt to use objects
and names from your example:
Set CMD = Server.CreateObject("ADODB.Command")
Set CMD.ActiveConnection = conn 'Should already be open
CMD.CommandType = adCmdStoredProc
CMD.CommandText = "News_AttachDocument"
CMD.Parameters.Append(CMD.CreateParameter("@ID",adInteger,adParamInput,4))
CMD.Parameters.Append(CMD.CreateParameter("@Data",adVarBinary,adParamInput,16))
CMD.Parameters.Append(CMD.CreateParameter("@ContentType",adVarChar,adParamInput,50))
CMD.Parameters("@ID").Value = 1 'Your record ID
CMD.Parameters("@Data").Size = oStream.Size
CMD.Parameters("@Data").Value = oStream.Read()
CMD.Parameters("@ContentType").Value = 'Appropriate doctype
CMD.Execute()
Note that the important step here is setting both the size and value of the
blob parameter. You can then extract it with something along these lines:
Set RS = conn.Execute("SELECT NEWS_DATA, CONTENT_TYPE " _
"FROM News WHERE NEWS_ID = 1")
Response.ContentType = RS.Fields("CONTENT_TYPE").Value
Response.BinaryWrite(RS.Fields("NEWS_DATA").Value)
I have never had a problem doing it this way. There may be typos or other
syntax errors due to translation from my preferred JScript.
--
Dave Anderson
Unsolicited commercial email will be read at a cost of $500 per message. Use
of this email address implies consent to these terms. Please do not contact
me directly or ask me to contact you directly for assistance. If your
question is worth asking, it's worth posting.
| |
| znubbe@hotmail.com 2006-02-17, 6:55 pm |
|
Hi Bob,
Thanks for your reply, but unfortunately it is the same result :-(
Do you know if the insert action adds some kind of header to the data
that might corrupt it?
At the end of the gibberish it there is this text "Word-dokument
MSWordDocWord.Document.8" as well as "Microsoft Word 9.0s". Could it
be a unicode problem?
Best Regards,
Znubbe
| |
| Bob Barrows [MVP] 2006-02-17, 6:55 pm |
| znubbe@hotmail.com wrote:
> Hi Bob,
>
> Thanks for your reply, but unfortunately it is the same result :-(
>
> Do you know if the insert action adds some kind of header to the data
> that might corrupt it?
Not in my experience. We use aspSmartUpload so perhaps my experience is not
typical.
I know a header is inserted when storing a Word Doc in an Access Memo field
but I've never seen an issue with SQL Server.
>
> At the end of the gibberish it there is this text "Word-dokument
> MSWordDocWord.Document.8" as well as "Microsoft Word 9.0s". Could it
> be a unicode problem?
>
Maybe, but I doubt it.Sorry i can't help.
--
Microsoft MVP -- ASP/ASP.NET
Please reply to the newsgroup. The email account listed in my From
header is my spam trap, so I don't check it very often. You will get a
quicker response by posting to the newsgroup.
| |
| Znubbe 2006-02-19, 6:55 pm |
| Hi again,
I changed to this line:
Response.AddHeader "Content-Disposition",
"attachment;filename=test.doc"
and it worked like charm :-) Not sure why inline doesn't work though.
|
|
|
|
|