473,659 Members | 3,117 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

2 New Member
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.Enc oding.UTF8, True)
Dim oEncoder As New UTF8Encoding()
Dim bytes As Byte() = oEncoder.GetByt es(fsw.ReadToEn d)

"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.FileS tream"C:\test.d oc", FileMode.Create )
oFileStream.Wri te(newArray, 0, newArray.GetUpp erBound(0) + 1)
oFileStream.Clo se()


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 5301
tmeers
16 New Member
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.Enc oding.UTF8, True)
Dim oEncoder As New UTF8Encoding()
Dim bytes As Byte() = oEncoder.GetByt es(fsw.ReadToEn d)

"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.FileS tream"C:\test.d oc", FileMode.Create )
oFileStream.Wri te(newArray, 0, newArray.GetUpp erBound(0) + 1)
oFileStream.Clo se()


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
orange123
2 New Member
Thanks for your help, but i just found something:

The answer is to use the filestream

Dim fileStream As New FileStream(strS tream, FileMode.Open, FileAccess.Read )
Dim len As Long = fileStream.Leng th
Dim fileAsByte(CInt (len)) As Byte
fileStream.Read (fileAsByte, 0, fileAsByte.Leng th)

'image field' = fileAsByte

fileStream.Clos e()
Jul 20 '07 #3

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

Similar topics

1
26269
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 StrConv function and I can Update the field correctly in T-SQL using: DECLARE @ptrval varbinary(16) SELECT @ptrval = TEXTPTR(BITS_data)
5
51206
by: JimMenees | last post by:
I know there's an easy answer to this which is escaping me: -------------------------------------------------------------------------- ----------------------- I have clickable thumb images which pass the image info to a window open script to display a larger version of the image along with a description of the image. The image info comes from the image element; the description string is in a hidden form var with the same name as the...
1
4386
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 image in a byte of what is in the database. I also want to be able to display this image in an <asp:image></asp:image> control. Is there any way to do this? Most of the examples that I've seen have used
2
5629
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 sending a image in the Ascii format using XML. ASCII is not useful for me to display the image. i need to convert it to BASE64 format... so i can go ahead and view the image. How do u convert ASCII to Base64 in C#.
1
21152
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 slow here and i am wondering if i can do it some other way to make it faster.....Any ideas public string ImageToBase64String(Image imageData ImageFormat format) { string base64; MemoryStream memory = new MemoryStream(); imageData.Save(memory,...
5
9622
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 text. The user may "convert image to text" and copy and paste the text displayed into any word document, or send the text displayed to the recipient's email address or can download the text file as a zipped text file that will be presented to...
29
2628
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 server back-end. I'm doing my best to learn about SQL server, and I plan to leave the front-end more or less as-is, just linking to the SQL server back end, but here's a basic question: The db has a front-end linked to two back-ends. One of the...
2
4228
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 this. Also - the citrix folks do not want us to keep the FE in Access as the queries and other activities consume a lot of power. The users will be in 3 different offices across the globe all accessing the 1 Oracle DB in Citrix. Does anyone have...
5
9399
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 Starting at 01/01/1900, 730395 would make the min year 3901? Thanks for any help.
0
8428
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
8335
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,...
1
8528
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8627
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5649
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
4175
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4335
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1976
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1737
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.