[ASP. NET] uploading files in ASP. NET

Source: Internet
Author: User

Use the "htmlinputfile" control to upload files.
Set "enctype =" multipart/form-Data "in the form label ""
For example, <Form ID = "form1" enctype = "multipart/form-Data" runat = "server">
<Input type = "file" id = "file1" runat = "server"/>
// Place a button
</Form>

BackgroundCodeIs:
Click to processProgramIs:

// check that the uploaded file is not empty
If (file1.postedfile! = NULL)
{< br> string Nam = file1.postedfile. filename;
// get the last ". "index
int I = Nam. lastindexof (". ");
// get the file extension
string newext = Nam. substring (I);
// here, the file name is automatically named Based on the date and file size to ensure that the file name is not repeated
datetime now = datetime. now;
string newname = now. ticks. tostring ();
// save the file to your desired directory, which is the upload directory under the IIS root directory. you can change.
// Note: Server is used here. mappath () gets the absolute directory of the current file. in Asp.net, "" must be replaced with ""
file1.postedfile. saveas (server. mappath (".. /upload/"+ newname + newext);

// Obtain the relevant attributes of the file: file name, file type, and file size.
// Fname. Text = file1.postedfile. filename;
// Fenc. Text = file1.postedfile. contenttype; format: "image/JPEG", "image/GIF", etc.
// Fsize. Text = file1.postedfile. contentlength. tostring ();
}

You can use the file name extension to determine the file type.

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.