Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Upload files (not images) to sql database

Status
Not open for further replies.

tembalena

Programmer
Apr 17, 2001
37
ZA
Hi, can someone show me how to upload a file (not image) to a sql2000 database, preferrably into an ntext or text field, and then how to display that file to the client browser without getting a popup message asking the user if they want to open or save the file.
Thanks so much.
 
tem: The most common way is just to use the HTMLInputFile control, since it can transfer 'any' type of file from the hard drive to the server. e.g.,

Upload button:

inpFile.PostedFile.SaveAs("c:\mydocs\myfile.xls")

<html>
...
<form EncType=&quot;multipart/form-data&quot; runat=&quot;server&quot;>

<input id=inpFileUp&quot; Type=&quot;File&quot; runat=&quot;server&quot;>

...

<asp:Button ... (on click --> inpFileUp.PostedFile...

This is typical, and standard. On IE and Netscape the input tag renders a text box accopanied by a Browse button that enables you to select a file to upload from your hard drive.
 
Sorry, I didn't make myself clear enough, I've sorted the issue of uploading the file, I'm now saving it as binary in an image field in my sql2k table. However, when I try to download it to the client (browser), having set the content-type and content-length, and using binarywrite(), I always get the download dialogue popping up, whereas I want the file to open in the browser without any popup messages to the user. It works fine with a text file, but anything other, eg. word document, causes the browser to ask the user to Open or save the file to local disk.
Any ideas on how to fix this?
Thanks
 
temb: Not sure of technique here - but here is a little cut and paste from Walther (2002); just in case there is a tidbit here for you --

Reading from a Binary File

To read from a binary file, you must create an instance of both the FileStream class and the BinaryReader class. You use the FileStream class to initialize the BinaryReader class.

The BinaryReader class contains seeral methods for reading a binary file. For example, if you want to read Visual Basic INtegers from a binary file, then you use the ReadInt32 method; if you want to read bytes, then you use the ReadByte method; if you want to read Boolean values, then you use the ReadBoolean method.

The following aspx page opens a binary file named myFile.data that contains Integer values and displays all the file's contents.

<% Import Namespace=&quot;System.IO %>
<%
Dim objFileStream As FileStream
Dim objBinaryReader As BinaryReader
Dim intCounter As Integer

objFileStream = New FileStream(MapPath(&quot;myFile.data&quot;), FileMode.Open)
objBinaryReader = New BinaryReader(objFileStream)

For intCounter = 0 to 50
Response.Write(&quot;<li> & objBinaryReader.ReadInt32())
Next

objBinaryReader.Close
%>

...I suppose your procedure doesn't involve serialization? I'll keep my eyes open...I have code pertaining to downloading images to a HTML page but I store all of my images on the hard drive - easy to retrieve - much better, if you can get away with it, then to open and retrieve from the database -- however, I have not heard all arguments on the advantages of storing them as binary (size?) v. as a file.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top