Simple example of uploading files using ASP. NET

Source: Internet
Author: User

Example

The code is as follows: Copy code

<Html>
<Title> demo of ASP. NET simple file upload program </title>
<Body>
<Script language = VB runat = server>
Sub uploadfile (sender as object, e as eventargs)
If fileup. postedfile. contentlength = 0 Then
Uptype. text = "select the file to upload !! "
Else
Dim filesplit () as string = split (fileup. postedfile. filename ,"")
Dim filename as string = filesplit (filesplit. length-1)
Fileup. postedfile. saveas (server. mappath (".") & "& filename)
Uptype. text = "File name:" & fileup. postedfile. filename & "<br> "&_
"File size:" & fileup. postedfile. contenttype & "<br> "&_
"File type:" & fileup. postedfile. contentlength
End If
End Sub
</Script>
<Form enctype = "multipart/form-data" runat = server>
<Input type = file id = fileup runat = server size = "20"> <p>
<Asp: button id = upload_button onclick = uploadfile text = "upload" runat = server/>
</Form>
<Asp: label id = uptype runat = server/>
</Body>
</Html>

During file upload, you can check the file name, file size, and file type. Currently, there is no Upload type restriction. You can add the type restriction by yourself. The above is just a test, next I will look at a more detailed file upload example.

The code is as follows: Copy code

Using System;
Using System. Data;
Using System. Configuration;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. IO;

Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
    {

    }
/// <Summary>
/// Upload the file to the specified folder
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Protected void btnSure_Click (object sender, EventArgs e)
    {
// String phName = this.txt Name. Text;
// String phType = this. ddlType. SelectedValue;

If (this. myFile. PostedFile! = Null) // myFile is the name of the upload control
        {
String photoName1 = myFile. PostedFile. FileName; // get the initial file name
Int I = photoName1.LastIndexOf ("."); // Retrieve the index of the last "." in the file name
String newext = photoName1.Substring (I); // get the file extension
If (newext! = ". Gif" & newext! = ". Jpg" & newext! = ". Jpeg" & newext! = ". Bmp" & newext! = ". Png ")
            {
Response. Write ("The file format is incorrect! ");
Response. End ();
            }
DateTime now = DateTime. Now; // Obtain the system time

String classid = DateTime. Now. Year. ToString ();

// Determine whether the current year folder exists under the path based on the year; otherwise, a folder with the year will be created
If (! Directory. exists (HttpContext. current. server. mapPath ("photos/") + "//" + classid) // HttpContext. current. server. mapPath (relative path): converts the relative path to the absolute path on the server. File. Exists (absolute path): Check whether there is a File or directory pointed to by the absolute path.
            {
System. IO. directory. createDirectory (@ HttpContext. current. server. mapPath ("photos/") + "//" + classid); // System. IO. directory. createDirectory: create an absolute path folder.
            }
String photoName2 = now. millisecond. toString () + "_" + myFile. postedFile. contentLength. toString () + newext; // rename the file, in milliseconds + file size + extension
MyFile. postedFile. saveAs (Server. mapPath ("photos //" + classid + "//" + photoName2); // Save the file to the path and use Server. mapPath () gets the absolute directory of the current file. in asp.net, "/" must be replaced "//"
        }

    }
/// <Summary>
/// This is an example of creating a folder in a directory specified by the button control, which can be uploaded to different folders by year, month, or by different people
/// </Summary>
/// <Param name = "sender"> </param>
/// <Param name = "e"> </param>
Protected void btnCreateBag_Click (object sender, EventArgs e)
    {
String classid = DateTime. Now. Year. ToString ();
If (! Directory. exists (HttpContext. current. server. mapPath ("photos/") + "//" + classid) // HttpContext. current. server. mapPath (relative path): converts the relative path to the absolute path on the server. File. Exists (absolute path): Check whether there is a File or directory pointed to by the absolute path.
        {
System. IO. directory. createDirectory (@ HttpContext. current. server. mapPath ("photos/") + "//" + classid); // System. IO. directory. createDirectory: create an absolute path folder.

        }
Else
        {
Response. Write ("<script language = javascript> alert ('folder already exists ') </script> ");
        }

    }
}

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.