473,769 Members | 2,116 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# Web App: Retrieving documents stored in SQL

55 New Member
Hi,

I need some help retrieving documents I have inserted into SQL in an Image type field. What I want to get is that when the user clicks the view button it retrieves the document in the format it was saved as. Code is below:

Expand|Select|Wrap|Line Numbers
  1. SqlConnection connSQL = new SqlConnection(conn);
  2. connSQL.Open();
  3.  
  4. SqlCommand cmdHeader = new SqlCommand("SELECT Title, Attachment, Attachment_Type FROM MDM_REQUEST_ATTACHMENTS Attachment_ID = 10", connSQL);
  5. cmdHeader.CommandType = CommandType.Text;
  6. SqlDataReader rdrRead = cmdHeader.ExecuteReader();
  7. rdrRead.Read();
  8.  
  9. Response.ContentType = rdrRead[2].ToString();
  10. Response.AddHeader("Content-Disposition", "attachment; filename=" + rdrRead.GetString(0));
  11. Response.Write((byte[])rdrRead[1]);            
  12. Response.End();
  13.  
At the moment this just opens up Microsoft Word and displays System.Byte[] rather than the word "Test" which was saved in the file befor it was inserted :(

Many thanks in advance.
Dec 10 '08 #1
2 2002
Curtis Rutland
3,256 Recognized Expert Specialist
Perhaps try Response.Binary Write. Or save it to a file and use Response.WriteF ile.
Dec 10 '08 #2
mldisibio
190 Recognized Expert New Member
Expand|Select|Wrap|Line Numbers
  1. // ACKNOWLEDGEMENT: http://www.c-sharpcorner.com/Code/2004/March/DownloadingFromMemStream.asp
  2. private void Page_Load(object sender, System.EventArgs e){
  3. //Create and populate a memorystream with the contents of the database table
  4. System.IO.MemoryStream mstream = GetData();
  5.  
  6. //Convert the memorystream to an array of bytes.
  7. byte[] byteArray = mstream.ToArray();
  8.  
  9. //Clean up the memory stream
  10. mstream.Flush();
  11. mstream.Close();
  12. // Clear all content output from the buffer stream
  13. Response.Clear();
  14.  
  15. // Add a HTTP header to the output stream that specifies the default filename for the browser's download dialog
  16. Response.AddHeader("Content-Disposition", "attachment; filename=foo.xls");
  17.  
  18. // Add a HTTP header to the output stream that contains the content length(File Size). 
  19. // This lets the browser know how much data is being transfered
  20. Response.AddHeader("Content-Length",
  21. byteArray.Length.ToString());
  22.  
  23. // Set the HTTP MIME type of the output stream
  24. Response.ContentType = "application/octet-stream";
  25.  
  26. // Write the data out to the client.
  27. Response.BinaryWrite(byteArray);
  28. Response.Flush();
  29. }
Dec 10 '08 #3

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

Similar topics

3
2949
by: Dave Smithz | last post by:
Hi There, Being quite new to MS-SQL I would like to ask if there is a general opinion of what approach should be taken to storing things like external documents and images in databases. Should the actual files be stored within the database, or instead should links to the files on a file server or something similar be stored instead. For the end user I imagine it is easier to have everything stored within the
35
2554
by: NickName | last post by:
I understand it's easy to list all saved queries of a given Access database via Msysobjects system table. However, I have not seen any posting over this NG or other similar ones that also include SQL statement(content) of these queries, though I've noticed some VB code for that. Is that because it's simply impossible to get a query content (not query resultset) from a SQL stmt? Thanks in advance.
7
2918
by: jsale | last post by:
I'm currently using ASP.NET with VS2003 and SQL Server 2003. The ASP.NET app i have made is running on IIS v6 and consists of a number of pages that allow the user to read information from the database into classes, which are used throughout the application. I have made class collections which, upon reading from the DB, create an instance of the class and store the DB values in there temporarily. My problem is that if user1 looks at...
3
1767
by: EYIII | last post by:
Is it possible to retrieve the "created by" identity and "modified by" identity for a file (e.g. word doc, .pdf, report, etc) using .NET?
11
3934
by: Peter Afonin | last post by:
Hello, I have Word documents stored in the Oracle database in the BLOB field that I need to retrieve. I've found the way of doing it: While dr.Read sName = dr("TE_PM_ATTACH_NAME") bLob = dr.GetOracleLob(1)
6
1426
by: NickP | last post by:
Hi there, I am implementing a simple "/U" command line argument for a .NET application that instructs the application to delete the following folders... My.Computer.FileSystem.SpecialDirectories.CurrentUserApplicationData My.Computer.FileSystem.SpecialDirectories.AllUsersApplicationData The application deletes these folders successfully then exits. Even
6
4666
by: shantanu | last post by:
Hi All, I have a requirement to develop a search engine based on some search criteria that will search for the string or statement in all the documents uploaded in the website. The search result will be displayed same as the Google Group search, with highlighted texts. I have few questions: 1. What will be the logic to implement? 2. The documents that will be uploaded in the website will be saved in the datbase or not?
1
2991
by: ahammad | last post by:
This application opens a Word document for editing. I need to keep the file path of the Word document even after it has been closed. I tried using Word event handlers but I couldn't figure it out, so I came up with this solution: _Application wordAppBlank; CString idBlank("Word.Application"); BOOL statusBlank = wordAppBlank.CreateDispatch(idBlank); CString blankFilePath; _Document myDocBlank;
46
2173
by: RAZZ | last post by:
Hello, Can anyone suggest me solution? I Need to manage different types of documents (doc,xls,ppt etc) in server. I have folder structure to maintain these documents in server. Say folder1 is having all doc files; folder2 is having all xls files and so on. Now these documents should not be able to get access through the url
0
9422
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,...
0
10038
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9987
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
9857
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
8867
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7404
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6662
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();...
1
3952
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3558
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.