File Upload
1. In form, set enctype to "multipart/form-Data ":
2. Determine whether a file has been uploaded:
When the user does not select any file to be uploaded, that is, when the text box in the htmlinputfile control is empty and the upload button is clicked, The file1.postedfile object obtained on the server end is not null but object-specific, therefore, you cannot use (file1.postedfile = NULL) to determine whether a file has been uploaded. Use (file1.postedfile. contentlength! = 0 ).
Iii. Determine the mimie type of the uploaded file:
After uploading a file, you can use file1.postedfile. contenttype to read the mimie type of the file, which is obtained by the system by the extension name of the uploaded file.
4. Save the uploaded file:
1. The file can be saved through 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;
If (file. exists (PATH) = true)
{
Label1.text = "the file you are uploading already exists on the server:" + filename;
Return;
}
File1.postedfile. saveas (PATH );
Mystr. append ("saved! ");
Mystr. append ("");
Label1.text = mystr. tostring ();