Save and output any type of file (C #) in SQL Server

xiaoxiao2021-03-06  124

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>



file name:

B>

file:

b> ?

form>

Once we are uploaded, we can browse the files: only set the MIME type of the page, and then output with the binaryWrite () of the Response object.

ShowuploadFile.aspx

?

<%

? @PAGE? Language

=

"

C #

"

?

%>

<%

? @Import? Namespace

=

"

SYSTEM.IO

"

?

%>

<%

? @? Import? Namespace

=

"

System.data

"

?

%>

<%

? @? Import? Namespace

=

"

System.data.sqlclient

"

?

%>

<

Script? Runat

=

"

Server

"

>

Private

?

Void

• Page_load (Object? Sender,? Eventargs? E)?

{STRING? SQL = "SELECT? *? from? Testfiles"; sqlconnection? connection? =? new? sqlConnection ("server = .; uid = sa; pwd =; database = testuploadFile"; sqlcommand? command? =? new SQLCOMMAND (SQL,? Connection); connection.open (); filelist.datasource? =? Command.executeReader (); filelist.database (); connection.close ();

Script> <% #? DataBinder.eval (container.DataItem,? "MyFileName")?%> B>

ItemTemplate>

ASP: TemplateColumn> <%? DataBinder.eval (Container.DataItem,? "FileType")?%>

B>

ItemTemplate>

ASP: TemplateColumn> View files

a>

B>

ItemTemplate>

ASP: TemplateColumn>

Column>

ASP: DATAGRID>

Form>

Showfile.aspx?

<%

? @PAGE? Language

=

"

C #

"

?

%>

<%

? @Import? Namespace

=

"

SYSTEM.IO

"

?

%>

<%

? @? Import? Namespace

=

"

System.data

"

?

%>

<%

? @? Import? Namespace

=

"

System.data.sqlclient

"

?

%>

<

Script? Runat

=

"

Server

"

>

Private

?

Void

• Page_load (Object? Sender,? Eventargs? E)?

{STRING? SQL = "SELECT? *? from? Testfiles? WHERE? ID? =? '"? ? request.queryString ["ID"]? ? "'"; sqlconnection? connection? =? new? sqlConnection "Server = .; uid = sa; pwd =; data = testuploadfile"); sqlcommand? Command? =? New? Sqlcommand (sql,? Connection); connection.open (); sqldataareader? DR? =? Command.executeReader ); if (Dr.Read ()) {???? response.clear (); ???? response.addheader ("Content-Type", DR ["filetype"]. TOSTRING ());??? ? Response.binaryWrite ((byte []) DR ["myfile"]);} Dr.close (); connection.close ();} script>

It should be noted that the EXE, ZIP file, etc. also have further processed to download it directly.

转载请注明原文地址:https://www.9cbs.com/read-127053.html

New Post(0)