Author: Meng will come from: [Meng] will be the best of the world
We can save any type of file to SQL Server, before performing an example, set the test form, testfile.sql:
IF
? EXISTS? (SELECT?
*
? from? dbo.sysObjects? Where? id?
=
? Object_id (n
'
[dbo]. [Testfiles]
'
????? and? ObjectProperty (ID,? n
'
Isusertable
'
)?
=
?
1
Drop? Table? [dbo]. [Testfiles] GOCREATE? TABLE? [DBO]. [Testfiles]? (???? [id]? [
int
]? Identity? (
1
?
1
)? NOT? NULL?, ???? [myfilename]? [Varchar]?
50
)? Collate? Chinese_prc_ci_as? Not? Null?, ???? [fileetype]? [Varchar]? (
50
)? Collate? Chinese_prc_ci_as? Not? Null?, ???? [myfile]? [Image]? Not? Null?)? ON? [Primary]? TEXTIMAGE_ON? [PRIMARY] GO
Create up to the uplist below:
Once the form is submitted, we use the HTMLINPUTFILE class's postedFile property to access the files we upload, use the properties and methods of the HTTPPostedFile class to read, save upload files, and get other information for upload files. Here we don't use the SaveAs method because it is used to save the file. We have to save the data into the database, we use the InputStream property, which is used to initialize the stream to read our data. At the same time, we use ContentLength to read the file size, and ContentType reads the file type. Then create a BYTE array, save the file stream into this array, and save it to the database.
Here is the complete code [CS version] UPLOADFILE.ASPX:
<%
? @PAGE? Language
=
"
C #
"
?
%>
<%
? @Import? Namespace
=
"
SYSTEM.IO
"
?
%>
<%
? @? Import? Namespace
=
"
System.data
"
?
%>
<%
? @? Import? Namespace
=
"
System.data.sqlclient
"
?
%>
<
Script? Runat
=
"
Server
"
>
public
?
Void
UPLOADBTN_CLICK? (Object? Sender,? Eventargs? E)
{// get the submitted file stream? FileDataStream? =? Myfile.postedfile.inputstream; // Get file size int? Filelength? =? Myfile.postedfile.contentLength; // Create array Byte []? FileData? =? New? Byte [fileLength]; // Pack the file stream to the array FileDataStream.read (FileData, 0, FileLength); // Get file name string? filetitle? =? myfilename.value; // Get file type string? filetype? =? MyFile.postedFile.ContentType; // Build a database connection, SQL statement, create parameter sqlconnection? Connection? =? New? SqlConnection ("server = .; uid = sa; pwd =; dataBase = testuploadFile"; sqlcommand? Command? = ? New? SQLCommand? ("INSERT? INTO? Testfiles? (MyFileName, MyFile, FileType"? ? "VALUES? (@myfilename, @ myfile, @ filetype)" ,? connection); sqlparameter? paramtitle? =? new ? Sqlparameter? ("@ Myfilename",? Sqldbtype.varchar, 35) ;? paramtitle.value? =? Filetitle; command.Parameters.add (paramtitle); SQLParameter? Paramdata? =? New? Sqlparameter? ("@ Myfile" , Sqldbtype.image); paramdata.value? =? Filedata; command.Parameters.Add (paramdata); sqlparameter? Paramtype? =? New? Sqlparameter? ("@ Filetype",? Sqldbtype.varchar, 25);? Paramtype .Value? =? FileType; command.parameters.add (paramtype); // play Open, execute the query connection.open (); command.executenonquery (); connection.close (); message.text = "Your file has been successfully uploaded"; MyFileName.Value? =? "" "" Script>