File Upload
I. Be sure to set ENCTYPE to "Multipart / Form-Data":
II. Determine 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 Object, so you can't use (file1.postedfile == null) to determine whether the file is uploaded, use (file1.postedfile.contentLength! = 0) to determine the better
III. 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 ("File Length:" File1.postedFile.contentLength.toString ());
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!");
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 reading: byte [] filecont = new byte [file1.postedfile.contentLength]; file1.postedfile.inputstream.read (FileCont, 0, file1.postedfile .ContentLength; then assign this byte array fileCont to the parameters of the binary field of the database, write to the database. file download
I. The server outputs the corresponding HTTP Response Headers information via the response, and the data to download files to the client, HTTP Response Headers is in the HTML file: http-equiv denotes the name of Headers, Content means the value of this Headers
Second. 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.addhead ("content-disposition", "attachment; filename =" filename); Content-Disposition HTTP Response Header allows the designated documentation information. 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
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 file output directly:
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);