FileUpload control, fileupload
FileUploadWidget
Attribute:
FileName:Get the uploaded file name
HasFile:Select (exists) the file to be uploaded
ContentLength:Obtains the size of the climbing file, in bytes)
Method:
Server. MapPath ():Obtain the physical path on the server
SaveAs ():Save the file to the specified folder.
Note: by default, the size of the uploaded file is limited4 MBYou can modify the default settings through web. config. comments (which is a global configuration ).
You can also modify the web. config file to change the application upload restrictions.
For example:
Xml Code
- <HttpRuntimeMaxRequestLength = "10240" executionTimeout = "150" enable = "true"/>
<HttpRuntime maxRequestLength = "10240" executionTimeout = "150" enable = "true"/>
Element explanation:
MaxRequestLength:Sets the size of the uploaded file, in kb.
ExecutionTimeout:Maximum number of seconds allowed for request execution. This function takes effect only when the Debug attribute in the Compilation element is false.
Enable:Specifies whether to enable the application domain (AppDomain) at the current node and subnode level to accept incoming requests. If it is False, the application is actually closed. The default value is True.
In general, whether to allow users to access the website (whether to allow Http requests)
Note: If the size of the uploaded file exceeds maxRequestLength, the page cannot be displayed.
For example:
Check the file upload type:
1). Client check (implemented in javascript)
2). Server check
Example:
Interface reference:
Javascript:
Js Code
- Function checkType (){
- // Obtain the value of the uploaded file
- Var fileName = document. getElementById ("FileUpLoad1"). value;
- // Returns the position at which the substring of the String object appears.
- Var seat = fileName. lastIndexOf (".");
- // Return the substring at the specified position in the String object and convert it to lowercase.
- Var extension = fileName. substring (seat). toLowerCase ();
- // Determine the format of the file to be uploaded
- // If (extension! = ". Jpg" & extension! = ". Jpeg" & extension! = ". Gif" & extension! = ". Png" & extension! = ". Bmp "){
- // Alert ("not supported" + extension + "File Upload! ");
- // Return false;
- //} Else {
- // Return true;
- //}
- Var allowed = [". jpg", ". gif", ". png", ". bmp", ". jpeg"];
- For (var I = 0; I <allowed. length; I ++ ){
- If (! (Allowed [I]! = Extension )){
- Return true;
- }
- }
- Alert ("not supported" + extension + "format ");
- Return false;
- }
Function checkType (){
// Obtain the value of the uploaded file
Var fileName = document. getElementById ("FileUpLoad1"). value;
// Returns the position at which the substring of the String object appears.
Var seat = fileName. lastIndexOf (".");
// Return the substring at the specified position in the String object and convert it to lowercase.
Var extension = fileName. substring (seat). toLowerCase ();
// Determine the format of the file to be uploaded
// If (extension! = ". Jpg" & extension! = ". Jpeg" & extension! = ". Gif" & extension! = ". Png" & extension! = ". Bmp "){
// Alert ("not supported" + extension + "File Upload! ");
// Return false;
//} Else {
// Return true;
//}
Var allowed = [". jpg", ". gif", ". png", ". bmp", ". jpeg"];
For (var I = 0; I <allowed. length; I ++ ){
If (! (Allowed [I]! = Extension )){
Return true;
}
}
Alert ("not supported" + extension + "format ");
Return false;
}
. Aspx:
Html code
- <HtmlXmlns = "http://www.w3.org/1999/xhtml">
- <HeadRunat = "server">
- <Title>No title page</Title>
- </Head>
- <Body>
- <FormId = "form1" runat = "server">
- <Div>
- <Asp: FileUploadID = "FileUpLoad1" runat = "server"/>
- <Br />
- <Asp: ButtonID = "btnFileUpload" runat = "server"
- OnClick = "btnFileUpload_Click" Text = "File Upload"
- OnClientClick = "return checkType ()"/>
- <Asp: LabelID = "lblMessage" runat = "server"> </Asp: Label>
- </Div>
- </Form>
- </Body>
- </Html>
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> No title page </title>
</Head>
<Body>
<Form id = "form1" runat = "server">
<Div>
<Asp: FileUpload ID = "FileUpLoad1" runat = "server"/>
<Br/>
<Asp: Button ID = "btnFileUpload" runat = "server"
OnClick = "btnFileUpload_Click" Text = "File Upload"
OnClientClick = "return checkType ()"/>
<Asp: Label ID = "lblMessage" runat = "server"> </asp: Label>
</Div>
</Form>
</Body>
</Html>
. Aspx. cs:
C # code
- // File upload button click event
- Protected void btnFileUpload_Click (object sender, EventArgs e)
- {
- If (FileUpLoad1.HasFile)
- {
- // Determine whether the file is smaller than 10 Mb
- If (FileUpLoad1.PostedFile. ContentLength <10485760)
- {
- Try
- {
- // Upload the file and specify the path of the upload directory
- FileUpLoad1.PostedFile. SaveAs (Server. MapPath ("~ /Files /")
- + FileUpLoad1.FileName );
- /* Note-> Why not: FileUpLoad1.PostedFile. FileName
- * But: FileUpLoad1.FileName?
- * The former is the name that obtains the complete limitation of the client (full path of the client ).
- * The latter FileUpLoad1.FileName only obtains the file name.
- */
- // Of course, the upload statement can also be written like this (seemingly nonsense ):
- // FileUpLoad1.SaveAs (@ "D: \" + FileUpLoad1.FileName );
- LblMessage. Text = "Upload successful! ";
- }
- Catch (Exception ex)
- {
- LblMessage. Text = "An exception occurred and cannot be uploaded! ";
- // LblMessage. Text + = ex. Message;
- }
- }
- Else
- {
- LblMessage. Text = "the size of the uploaded file cannot exceed 10 MB! ";
- }
- }
- Else
- {
- LblMessage. Text = "file not selected! ";
- }
- }