FileUpload control, fileupload

Source: Internet
Author: User

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

  1. <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

  1. Function checkType (){
  2. // Obtain the value of the uploaded file
  3. Var fileName = document. getElementById ("FileUpLoad1"). value;
  4. // Returns the position at which the substring of the String object appears.
  5. Var seat = fileName. lastIndexOf (".");
  6. // Return the substring at the specified position in the String object and convert it to lowercase.
  7. Var extension = fileName. substring (seat). toLowerCase ();
  8. // Determine the format of the file to be uploaded
  9. // If (extension! = ". Jpg" & extension! = ". Jpeg" & extension! = ". Gif" & extension! = ". Png" & extension! = ". Bmp "){
  10. // Alert ("not supported" + extension + "File Upload! ");
  11. // Return false;
  12. //} Else {
  13. // Return true;
  14. //}
  15. Var allowed = [". jpg", ". gif", ". png", ". bmp", ". jpeg"];
  16. For (var I = 0; I <allowed. length; I ++ ){
  17. If (! (Allowed [I]! = Extension )){
  18. Return true;
  19. }
  20. }
  21. Alert ("not supported" + extension + "format ");
  22. Return false;
  23. }

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

  1. <HtmlXmlns = "http://www.w3.org/1999/xhtml">
  2. <HeadRunat = "server">
  3. <Title>No title page</Title>
  4. </Head>
  5. <Body>
  6. <FormId = "form1" runat = "server">
  7. <Div>
  8. <Asp: FileUploadID = "FileUpLoad1" runat = "server"/>
  9. <Br />
  10. <Asp: ButtonID = "btnFileUpload" runat = "server"
  11. OnClick = "btnFileUpload_Click" Text = "File Upload"
  12. OnClientClick = "return checkType ()"/>
  13. <Asp: LabelID = "lblMessage" runat = "server"> </Asp: Label>
  14. </Div>
  15. </Form>
  16. </Body>
  17. </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

  1. // File upload button click event
  2. Protected void btnFileUpload_Click (object sender, EventArgs e)
  3. {
  4. If (FileUpLoad1.HasFile)
  5. {
  6. // Determine whether the file is smaller than 10 Mb
  7. If (FileUpLoad1.PostedFile. ContentLength <10485760)
  8. {
  9. Try
  10. {
  11. // Upload the file and specify the path of the upload directory
  12. FileUpLoad1.PostedFile. SaveAs (Server. MapPath ("~ /Files /")
  13. + FileUpLoad1.FileName );
  14. /* Note-> Why not: FileUpLoad1.PostedFile. FileName
  15. * But: FileUpLoad1.FileName?
  16. * The former is the name that obtains the complete limitation of the client (full path of the client ).
  17. * The latter FileUpLoad1.FileName only obtains the file name.
  18. */
  19. // Of course, the upload statement can also be written like this (seemingly nonsense ):
  20. // FileUpLoad1.SaveAs (@ "D: \" + FileUpLoad1.FileName );
  21. LblMessage. Text = "Upload successful! ";
  22. }
  23. Catch (Exception ex)
  24. {
  25. LblMessage. Text = "An exception occurred and cannot be uploaded! ";
  26. // LblMessage. Text + = ex. Message;
  27. }
  28. }
  29. Else
  30. {
  31. LblMessage. Text = "the size of the uploaded file cannot exceed 10 MB! ";
  32. }
  33. }
  34. Else
  35. {
  36. LblMessage. Text = "file not selected! ";
  37. }
  38. }

 

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.