Upload and download files in ASP. NET

Source: Internet
Author: User


// Check that the uploaded file is not empty
If (File1.PostedFile! = Null)
{
String nam = File1.PostedFile. FileName;
// Obtain the index of the last "." In the file name (including the path)
Int I = nam. LastIndexOf (".");
// Get the file extension
String newext = nam. Substring (I );
// Here, I automatically name the File Based on the date and file size to ensure that the file name is not repeated
DateTime now = DateTime. Now;
String newname = now. DayOfYear. ToString () + File1.PostedFile. ContentLength. ToString ();
// Save the file to the directory you want. This is the upload directory under the IIS root directory. You can change it.
// Note: Here I use Server. MapPath () to retrieve the absolute directory of the current file. In asp.net, "" must be replaced ""
File1.PostedFile. SaveAs (Server. MapPath ("upload" + newname + newext ));
This. HyperLink1.NavigateUrl = "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;
// Fsize. Text = File1.PostedFile. ContentLength. ToString ();
}

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.