Upload a simple file (without a database)

Source: Internet
Author: User

Protected void cmdUpload_Click (object sender, EventArgs e)
{
// Check if a file was submitted.
If (Uploader. PostedFile. ContentLength! = 0)
{
Try
{
If (Uploader. PostedFile. ContentLength> 1048576)
{
// Limit the file size here
// File Upload is limited to 4 MB
// If the value is larger, use the maxRequestLength attribute set in
LblStatus. Text = "Too large. This file is not allowed ";
}
Else
{
// Upload to the Upload path
String destDir = Server. MapPath ("./Upload ");

 

// Get the file name
String fileName = System. IO. Path. GetFileName (
Uploader. PostedFile. FileName );


String destPath = System. IO. Path. Combine (destDir, fileName );

// Save it to the server
Uploader. PostedFile. SaveAs (destPath );
LblStatus. Text + = "Thanks for submitting your file .";

// Display the content. If the file is too large, do not use ReadToEnd (),
StreamReader r = new StreamReader (Uploader. PostedFile. InputStream );
LblStatus. Text + = r. ReadToEnd ();
R. Close ();
}
}
Catch (Exception err)
{
LblStatus. Text = err. Message;
}
}

}

 

We can create a globally unique identifier.

Private string GetFileName ()
{
// Create a unique filename.
String fileName = @ "Log \ user." +
Guid. NewGuid (). ToString ();

 

// Put the file in the current web application path.
Return Path. Combine (Request. PhysicalApplicationPath, fileName );
}

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.