Store and call Word files in the ASP.NET program

xiaoxiao2021-03-06  68

This article adopts an instance to explain how to cooperate with SQL Server2000 in the ASP.NET program (not using VBA).

(1) Establish a database

First, we create a table in the database, there are three fields, FileName (VARCHAR, 50), PostTime (DateTime, 8), FileContent (Image, 16), store file name, upload time, and Word file specific content, respectively. Where filename is the primary key. The specific SQL script is as follows:

Create Table [DBO]. [Word] (

[filename] [varchar] (50) collate chinese_prc_ci_as not null,

[posttime] NOT NULL,

[filecontent] [image] not null

) On [primary] textImage_on [primary]

(2) Upload and store the Word file

Create an ASP.NET web application in VS.NET, add the following controls in the interface

Control type ID text Description Label Label1 Please enter the title of the document

Label Label2, select a specific document

File Field file1

Upload control (to convert this HTML control into server control) TextBox Name_TextBox

Used to enter document title Button BTN_OK upload file

Button btn_get read file

HyperLink Hyperlink1 opens to open a Word document

When you upload a file, you first find the file you want to upload by uploading, then get the size of the file, and finally write the database in the form of a stream, the specific code is:

Private void btn_ok_click (Object Sender, System.EventArgs E)

{

String name = name_textbox.text;

/ / Receive upload files

Street filestream = file1.postedfile.inputstream;

/ / Get the size of the upload file byte

INT length = file1.postedfile.contentLENGTH;

Byte [] WordData = New byte [length];

// Read bytes from the stream and write WordData

INT N = FileStream.read (WordData, 0, Length);

/ / Get the current time

DateTime Time = DATETIME.NOW;

//Connect to the database

SqlConnection conn = new sqlConnection ();

Conn.connectionstring = "Workstation ID = tianchunzhu; packet size = 4096; integrated security = SSPI; data source = tianchunzhu; persist security info = false; initial catalog = test";

SQLCommand cmd = new sqlcommand ();

cmd.connection = conn;

Cmd.comMandtext = "Insert Into Word (filename, posttime, filecontent) Values ​​(@ filename, @ posttime, @ filecontent);

SqlParameter NameParam = New SqlParameter ("@ filename", system.data.sqldbtype.varchar, 50); NameParam.Value = name;

Cmd.Parameters.Add (NameParam);

SQLParameter TimeParam = New SqlParameter ("@ PostTime", System.Data.sqldbtype.Datetime, 8);

TimeParam.Value = TIME;

Cmd.Parameters.Add (TimeParam);

// Add Word file

SQLParameter ContentParam = New Sqlparameter ("@ filecontent", system.data.sqldbtype.image); 1 // see the last annotation

ContentParam.Value = WordData;

Cmd.Parameters.Add (ContentParam);

Cn.open ();

cmd.executenonquery ();

CONN.CLOSE ();

}

Note 1: This is not possible to predict the size of the file in advance, so you don't have to specify the SIZE parameter. If you want to control the size of the upload file, you can enter the Size parameter. If you specify 1000, upload a maximum of 1K Word document at the time of upload.

(3) Read the data from the database and restore to Word files

When reading data, first read the data into the buffer from the database, then write the final file from the buffer. So first open up a buffer and set its size. Write the data in the buffer in the buffer and continue to read data to the buffer, until the last time will be buffered. The remaining data in the zone is written to the file, and the new Word document can be generated.

Since this part uses the input and output operation of the byte stream, you want to reference system.io namespace.

Here is the full code about this part:

Private Void BTN_GET_CLICK (Object Sender, System.EventArgs E)

{

//Connect to the database

SqlConnection conn = new sqlConnection ();

Conn.connectionstring = "Workstation ID = tianchunzhu; packet size = 4096; integrated security = SSPI; data source = tianchunzhu; persist security info = false; initial catalog = test";

SQLCommand cmd = new sqlcommand ();

cmd.connection = conn;

/ / Find read according to the file name specified in TextBox

cmd.commandtext = "SELECT FileContent from Word where filename = '" name_textbox.text.toString () "'";

FileStream Fs;

BinaryWriter BW;

// Set the maximum length allowed to be read to the buffer

INT buffersize = 100;

/ / Buffer to read bytes stream

Byte [] Outbyte = new byte [buffersize];

/ / Used to record the number of bytes already read

Long Reval;

// Index in the field starts reading here

Long StartIndex;

// FileStream object will package the relative path or absolute path string filepath = @ "c: /worddata.doc";

Cn.open ();

SqlDataReader Reader;

Reader = cmd.executeReader ();

While (Reader.Read ())

{

Fs = new filestream (filepath, filemode.openorcreate, fileaccess.write);

BW = new binarywriter (fs);

StartIndex = 0;

// read the byte stream into the Outbyte buffer and return the number of bytes read

Reval = reader.getbytes (0, StartIndex, Outbyte, 0, Buffersize);

/ / When the read byte stream reaches the maximum length allowed by the buffer, the data within the buffer is to be unloaded and the data is written to the file.

While (Reval == Buffersize)

{

BW.WRITE (OUTBYTE);

Bw.flush ();

/ / Reset the location that starts reading and continues reading and writing data

StartIndex = Buffersize;

Reval = reader.getbytes (0, StartIndex, Outbyte, 0, Buffersize);

}

// Write the last remaining data in the buffer to file

BW.WRITE (Outbyte, 0, (int) revAl-1);

Bw.flush ();

BW.CLOSE ();

fs.close ();

}

Reader.Close ();

CONN.CLOSE ();

}

The Word document will be regenerated by the path and name specified in FilePath. The name and path of the generated Word document can be specified in the FILEPATH according to the specific situation.

(4) Open Word documentation

This part of opening the Word document is not found to open the Word's valid approach through the Button button, but we can HYPERLINK control, just point your HyperLink control to the physical path to the Word document.

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

New Post(0)