Steps:
1. Write out the relative path position in the project first
String path = "Images/aaa.txt";
2. Map this relative path to absolute path
String endpath = Server.MapPath (path);
3. Save
Fileupload1.saveas ("Endpath");
Issue 1: file name is the same, easy to be overwritten
Method: Keep The original name of the file
String path = "images/" +fileupload1.filename;
Issue 2: file name is the same, multiple people upload the same file will be overwritten
Method: Name Add time splicing, user name splicing
String path= "images/" +datetime.now.tostring ("Yyyymmddhhmmss") +request.cookies["User". Value+fileupload1.filename;
Issue 3: Upload file format restrictions
Option 1:
Control properties: Accept = ". Jpg,.png,.jpeg";
Option 2:
Use JS to obtain the suffix name of the user's selected file and verify that it is consistent with the requirements
function
{ var fl = document.getElementById ("FileUpload1"); var aa = Fl.value.substr (fl.value.length-4, 4); if (AA! = ". txt" && aa! = ". png" && AA! = ". jpg"
{ alert ("file format selection is incorrect!) "); return false ; }};
Question 4: file too large
Method 1, expansion
Web. config configuration file:
In kilobytes, default 4096, not too large because it consumes server memory
Method 2, File size limit
1. C # End Limit
if (FileUpload1.PostedFile.ContentLength > (4096 * 1024)) However, it is not useful
2. Client JS limit
Fl.files[0].size can get the size of the selected file, B is the unit
function () { var fl = document.getElementById ("FileUpload1"); if (Fl.files[0].size > (1024x768 * 4)) { alert ("file exceeds 4MB"); return false ; }};
Chapter 6. File Upload