Document upload implementation (C #)

xiaoxiao2021-03-06  117

File Upload

1. Be sure to set ENCTYPE to "Multipart / Form-Data" in Form:

2. Judgment whether there is a file upload:

When the user does not select any file to be uploaded, the text box in the HTMLINPUTFILE control is empty. After the upload button is empty, the file1.postedFile object obtained at the server is not NULL, but there is an object, so it cannot be used (File1. PostedFile == NULL) To determine if the file is uploaded, use (file1.postedfile.contentLength! = 0) to determine the better

I. Judgment upload file MIMIE type:

After the file is uploaded, you can use file1.postedfile.contentType to read the MIMIE type of this file. This MIMIE type is obtained by the system through the verge of the upload file.

4. Save the uploaded file:

1. Files can be saved by file1.postedfile.saveas (path) // path is the physical path on the server.

IF (file1.postedfile.contentLength! = 0)

{

Stringbuilder mystr = new stringbuilder ();

MyStr.Append ("file name:" file1.postedfile.filename);

MyStr.Append ("
");

MyStr.Append ("File Type:" File1.PostedFile.contentType);

MyStr.Append ("
");

MyStr.Append ("File Length:" File1.postedFile.contentLength.toString ());

MyStr.Append ("
");

String path = server.mappath ("./"); // Current path

String filename = file1.postedfile.filename.substring (file1.postedfile.filename.lastIndexof ('//') 1);

Path = filename;

File.exists (PATH) == True)

{

Label1.Text = "There is already the file you are uploading on the server:" FileName;

Return;

}

File1.postedFile.saveas (PATH);

MyStr.Append ("Save!");

MyStr.Append ("
");

Label1.text = mystr.tostring ();

}

Else

{

Label1.text = "You didn't choose the file you want to upload or the uploaded file length is 0!";

}

2. The file can also be stored in the binary field of the database after binary read:

Byte [] filecont = new byte [file1.postedfile.contentLength]; file1.postedfile.inputStream.read (filecont, 0, file1.postedfile.contentLength);

This byte array FileCont then assigns the parameters of the binary field of the database, written to the database.

file download

I. The server outputs the corresponding HTTP Response Headers information via the response, and the data to which the file to download is sent to the client, and the HTTP Response Headers is in the form of the HTML file:

Http-Equiv represents the name of Headers, Content means this Headers value

II. First, to output the MIME type of the file:

Page.Response.addheader ("Content-Type", "MIME Type");

Third. Second, to output the open position and file name of the downloaded file:

Page.Response.addheader ("Content-Disposition", "Attachment; FileName =" FileName);

Content-Disposition's HTTP Response Header allows you to specify the information indicated by the document. With this header, you can specify a document separately (instead of opening in the browser), and can also be displayed according to the user's operation. If the user wants to save the document, you can also recommend a file name for the document. This suggestion name will appear in the File Name column of the Save AS dialog.

Open location:

Attachment - Represents as an attachment to the client, the client will open this file separately.

Inline - indicates that this file will be opened in the browser.

file name:

FileName - Indicates the file name sent to the client file.

4. Prepare file data sent to the client:

1. First transfer data from different types of sources to an array of Byte types, and then send it to the client via the response.binarywrite method:

1.1. Read the file to get the BYTE array: string filename; // Generate or get the file name to send to the client

String filepath = server.mappath ("./") filename; // hypothesis file in the current directory

IF (file.exists (filepath) == false)

{

// Don't have this file on the server

Return;

}

FILESTREAM MyFile = file.openread (filepath); // Read files into FILESTREAM

Byte [] filecont = new byte [myfile.length];

MyFile.read (filecont, 0, (int) myfile.length); // convert the content in the file into a BYTE array

1.2. Read in the binary field of the database: // Get the ID from the URL

String imageid = Request.QueryString ["IMG"];

/ / Build a query statement

String sqltext = "SELECT IMG_DATA, IMG_CONTENTTYPEWMAGE WHERE IMG_PK =" ImageId; SqlConnection Connection = New SqlConnection (ConfigurationSettings.AppSettings ["DSN"]. TOSTRING ());

Sqlcommand command = new SQLCOMMAND (SQLText, Connection);

Connection.open ();

SqlDataReader DR = command.executeReader ();

IF (Dr.Read ())

{

Byte [] filecont = (Byte []) DR ["IMG_DATA"];

}

Connection.Close ();

1.3. Read the file from the Internet: httpwebrequest mywebrequest = (httpwebRequest) WebRequest.create ("

http://www.via.com/aa.xls ");

HttpWebResponse mywebresponse = (httpwebresponse) MyWebRequest.getResponse ();

Stream readstream = MyWebResponse.getResponsestream ();

Byte [] bytes = new byte [readstream.length];

Bytes = readstream.read (bytes, 0, readstream.length);

The BYTE array of file content obtained by the above three methods can be used to output:

Page.Response.binaryWrite (filecont);

Page.Response.end ();

2. Read the file output directly: string filename; // Generate or get the file name to send to the client

String filepath = server.mappath ("./") filename; // hypothesis file in the current directory

IF (file.exists (filepath) == false)

{

// Don't have this file on the server

Return;

}

Page.Response.clear ();

Page.Response.addheader ("Content-Type", "Image / GIF"); / / According to MIME

Page.Response.addheader ("Content-Disposition", "Inline; filename =" filepath);

Page.Response.writefile (FilePath);

Page.Response.end ();

Content end //

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

New Post(0)