How to Use the FileUpload File Upload Control

Source: Internet
Author: User

// Creation Time:

// Created by: Yulin Lone Wolf

// Description: Instructions for using the FileUpload File Upload control (only part of it)

//You can upload images and txt files. Doc, wps, and audio files and video files. Powerful functions!

// Front-end code piece, set the Image Upload Interface

<Div>

<Asp: Label ID = "lbText" runat = "server" Font-Bold = "true" Font-Size = "20px" Text = "select the photo you want to upload">

</Asp: Label>

<Br/> <asp: Label ID = "lbImg" runat = "server" ForeColor = "Red"> </asp: Label> <br/>

<Asp: Label ID = "hh" runat = "server" Text = "Upload path:"> </asp: Label> <br/>

<Asp: TextBox ID = "txtFileName" runat = "server"> </asp: TextBox>

<Asp: FileUpload ID = "FileUpLoad1" runat = "server"/>

<Asp: Button ID = "btnUpload" runat = "server" Text = "Start upload" OnClick = "btnUpload_Click"/> <br/>

<Asp: Label ID = "hj" runat = "server" Text = "The uploaded image is as follows"> </asp: Label>

<Br/> <asp: Image ID = "img" runat = "server"/>

</Div>

// Background code for uploading images from the client to the server

Protected void btnUpload_Click (object sender, EventArgs e)

{// HasFile gets a value indicating whether the FileUpload control contains a file.

If ()

{

// PostedFile obtains the basic HttpPostedFile object by using the File Uploaded By the FileUpload control. Public HttpPostedFile

// PostedFile {get ;}

String fileContentType = FileUpLoad1.PostedFile. ContentType;

// You can also use file. Extension to determine the file type, as shown in figure

If (fileContentType = "image/jpeg" | fileContentType = "image/bmp" | fileContentType = "image/gif" |

FileContentType = "image/png ")

{

// Client file path

String name = FileUpLoad1.PostedFile. FileName;

// File name

String filename = file. Name;

// Server File Path

This.txt FileName. Text = filename;

String webFilePath = Server. MapPath ("Upload/" + filename );

// Determine whether the same file exists

If (! File. Exists (webFilePath ))

{

Try {

// Use SqveAs () to save the content of the uploaded file to the specified path on the Web Server

This. lbImg. Text = "prompt: the file" "+ filename +" is uploaded successfully! ";

This. img. ImageUrl = "Upload/" + filename;

}

Catch (Exception ex ){

This. lbImg. Text = "File Upload Failed because" + ex. Message;

}

}

}

Else

{This. lbImg. Text = "the file type does not match ";

}

}

}

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.