473,385 Members | 1,907 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Convert a document to image field in sql server 2005 and back again

Hi
Can someone help me please?

(I am doing all this code in VB.NET)

I have been trying to convert an existing document (word, excel, pdf, powerpoint, etc) to an image field) in my sql server database.
I then need to be able to pull out the data from the image field and open the document.

So far I have the following code:

PUT DOCUMENT IN DATABASE

Dim fsw As New StreamReader("C:\test.doc", System.Text.Encoding.UTF8, True)
Dim oEncoder As New UTF8Encoding()
Dim bytes As Byte() = oEncoder.GetBytes(fsw.ReadToEnd)

"DataTable field (to be inserted into image field)) with datatype of System.Byte()" = bytes


GET DOCUMENT FROM DATABASE

' Not sure if this line below should use some sort of UTF8Encoding when converting the image field to an array of bytes)
Dim newArray As Byte() = "DataTable field with datatype of System.Byte()" Dim oFileStream As FileStream = New System.IO.FileStream"C:\test.doc", FileMode.Create)
oFileStream.Write(newArray, 0, newArray.GetUpperBound(0) + 1)
oFileStream.Close()


When I open up the document, you can only see squares and boxes where the text should be displayed.

Am i incorrectly opening the document or saving the data to the database in the first place?

Many thanks!
Jul 20 '07 #1
2 5284
tmeers
16
Hi
Can someone help me please?

(I am doing all this code in VB.NET)

I have been trying to convert an existing document (word, excel, pdf, powerpoint, etc) to an image field) in my sql server database.
I then need to be able to pull out the data from the image field and open the document.

So far I have the following code:

PUT DOCUMENT IN DATABASE

Dim fsw As New StreamReader("C:\test.doc", System.Text.Encoding.UTF8, True)
Dim oEncoder As New UTF8Encoding()
Dim bytes As Byte() = oEncoder.GetBytes(fsw.ReadToEnd)

"DataTable field (to be inserted into image field)) with datatype of System.Byte()" = bytes


GET DOCUMENT FROM DATABASE

' Not sure if this line below should use some sort of UTF8Encoding when converting the image field to an array of bytes)
Dim newArray As Byte() = "DataTable field with datatype of System.Byte()" Dim oFileStream As FileStream = New System.IO.FileStream"C:\test.doc", FileMode.Create)
oFileStream.Write(newArray, 0, newArray.GetUpperBound(0) + 1)
oFileStream.Close()


When I open up the document, you can only see squares and boxes where the text should be displayed.

Am i incorrectly opening the document or saving the data to the database in the first place?

Many thanks!
Now while I'm not a pro at this and I use a different method to upload to the DB I use this but not very often as I have a handler that pulls it out for me with the name. But maybe this will help, mine is just in a file called get.aspx:

Expand|Select|Wrap|Line Numbers
  1. <%@ Import Namespace="System.Data" %>
  2. <%@ Import Namespace="System.IO" %>
  3. <%@ Page Language="VB" Debug="true" %>
  4. <script runat="server">
  5. Sub Page_Load(sender as object, e as EventArgs)
  6.  
  7.         Dim objConn As New SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ConnSting").ConnectionString)
  8.         Dim cmdText = "SELECT contenttype, [filename], Filedata, length FROM proj_file WHERE id='" & Request.QueryString("id") & "'"
  9.         Dim objcmd As New SqlClient.SqlCommand(cmdText, objConn)
  10.  
  11.         objConn.Open()
  12.         Dim objread As SqlClient.SqlDataReader = objcmd.ExecuteReader()
  13.  
  14.         If objread.Read() Then
  15.             Dim [filename] As String
  16.             Response.AddHeader("Content-Disposition", "attachment; filename=" + [filename])
  17.             Response.ContentType = objread("contenttype").ToString()
  18.             Response.BinaryWrite(CType(objread("filedata"), Byte()))
  19.             Response.End()
  20.         End If
  21.  
  22.         objConn.Close()
  23. End Sub
  24. </script>
The only issue I have with it is that it renames the file to get.(extention). I have come up wiht a different solution for my needs though check it out is you want to; http://gettinlucky.dyndns.org/shared

Hope this helps,
Tim
Jul 20 '07 #2
Thanks for your help, but i just found something:

The answer is to use the filestream

Dim fileStream As New FileStream(strStream, FileMode.Open, FileAccess.Read)
Dim len As Long = fileStream.Length
Dim fileAsByte(CInt(len)) As Byte
fileStream.Read(fileAsByte, 0, fileAsByte.Length)

'image field' = fileAsByte

fileStream.Close()
Jul 20 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: SD | last post by:
Hi, This is driving me nuts, I have a table that stores notes regarding an operation in an IMAGE data type field in MS SQL Server 2000. I can read and write no problem using Access using the...
5
by: JimMenees | last post by:
I know there's an easy answer to this which is escaping me: -------------------------------------------------------------------------- ----------------------- I have clickable thumb images which...
1
by: John Scott | last post by:
I am storing an image in an SQL database and have one field as an image datatype. I am also using a webservice to transport data. I want to be able to resize the image and pass back a thumbnail...
2
by: VenuGopal | last post by:
Hi Everyone, in my application i am interacting to a known server. i send a request and get a response from the server. i am using XML and HTTP here. part of the job is that the XYZ server is...
1
by: James dean | last post by:
The following code is the current method i have for converting an image to Base64.Is this the most efficient way to convert an image to base64 or are there others. The Image class's Save method is...
5
by: Socrates | last post by:
I am interested in developing an online utility that will enable users to copy and past any image (or upload any image on the internet) to the online utility, which will then convert the image to...
29
by: Jan | last post by:
Hi: I have an Access database that's been running (in one form or another) for a couple of different clients for a few years. Now a new client has requested that it be implemented with a SQL...
2
by: egoldthwait | last post by:
I need to convert a 17mb access 2000 db to Oracle and house it in a Citrix farm. The issue: we have never converted an Access Db to Oracle but can probably use Oracle's Workbench to assist with...
5
by: Artie | last post by:
Hi, I have a date field stored as an INT and need to convert to a date format mm/dd/yyyy. I'm having trouble determining what the starting date is. min(date) = 730395 max(date) = 733189 ...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...

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.