Asp.net implements File Upload

Source: Internet
Author: User

Find two simple asp.net File Upload codes on the Internet. If you need them, you can refer to them. It also limits asp, aspx, php, jsp, and other dangerous file uploads, take a look.

Using ASP. NET to upload files is very simple. Here I will provide an example to help my friends. The Code is as follows:

The Code is as follows: Copy code

<% @ Import Namespace = "System. IO" %>
<% @ Page Language = "C #" debug = "true" %>
<Html>
<Head>
<Title> Upload File, http://www.bKjia. c0m </title>
<Script language = "C #" runat = "server">
// This method is called when the "upload" button id pressed
Public void UploadFile (object sender, EventArgs E)
{
// Check that the uploaded file is not empty
If (myFile. PostedFile! = Null)
{
String nam = myFile. PostedFile. FileName;
// Retrieve the index of the last "." In the file name (including path)
Int I = nam. LastIndexOf (".");
// Get the file extension
String newext = nam. Substring (I );
// 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. DayOfYear. ToString () + myFile. PostedFile. ContentLength. ToString ();
// Save the file to the directory you want. This is the upfiles 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 "\"
MyFile. PostedFile. SaveAs (Server. MapPath ("\ upfiles \" + newname + newext ));
// Obtain the relevant attributes of the file: file name, file type, and file size.
Fname. Text = myFile. PostedFile. FileName;
Fenc. Text = myFile. PostedFile. ContentType;
Fsize. Text = myFile. PostedFile. ContentLength. ToString ();
}
}
</Script>
</Head>
<Body>
<Center>
<H3> File Upload instance from [url = http://www.bKjia. c0m/] http://www.bKjia. c0m <Form id = "uploderform" method = "post" action = "FileUpload. aspx" enctype = "multipart/form-data" runat = "server">
<Table border = "1" cellspacing = "2" cellpadding = "2">
<Tr> <td> <Tr>
<Td>
<Input type = "file" id = "myFile" runat = "server" NAME = "myFile">
</Td>
</Tr>
<Tr> <td>
<Input type = "button" value = "Upload" OnServerClick = "UploadFile" runat = "server" ID = "Button1" NAME = "Button1">
</Td> </tr>
</Table>
</Form>
<Br>
<Br>
<Table border = "1" cellspacing = "2">
<Tr> <td> <B> documents </B> </td>
<Td> & nbsp; </td>
</Tr>
<Tr>
<Td> file name: </td>
<Td> <asp: label id = "fname" text = "" runat = "server"/> </td> </tr>
<Tr>
<Td> file type: </td>
<Td> <asp: label id = "fenc" runat = "server"/> </td> </tr>
<Tr>
<Td> file size: (in bytes) </td>
<Td> <asp: label id = "fsize" runat = "server"/> </td> </tr>
</Table>
<Br>
<Br>
<Br>
</Center>
</Body>


This is the code program that implements file upload in asp.net. This is just an example. You can modify it as required.
Pay attention to security issues during file upload. Filter file types. Users cannot upload dynamic scripts (such as asp, asp.net, php, cgi, and exe. Otherwise, your website will have security issues. Easy to get hacked

 

The Code is as follows: Copy code

<% @ Page Language = "C #" %>
<% @ Import Namespace = "System. IO" %>
<Script language = "C #" runat = "server">

Public void uploadFile (object sender, EventArgs E ){
// Check that the uploaded file is not empty
If (inputFile. PostedFile. ContentLength> 0 ){
// Set the path to save the uploaded file
String strSaveDir = "./upload /";
String strName = inputFile. PostedFile. FileName;
// Obtain the index of the last "." In the file name (including the path)
Int intExt = strName. LastIndexOf (".");
// Get the file extension
String strExt = strName. Substring (intExt );
// Here, I automatically name the File Based on the date and file size to ensure that the file name is not repeated
/*
DateTime datNow = DateTime. Now;
String strNewName = datNow. DayOfYear. ToString () + inputFile. PostedFile. ContentLength. ToString () + strExt ;*/
// Retrieve the last "" index in the file name (including path)
Int intPath = strName. LastIndexOf ("\");
// Get the file name (excluding the path)
String strNewName = strName. Substring (intPath );
// 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 "\"
InputFile. PostedFile. SaveAs (Server. MapPath (strSaveDir + strNewName ));
// Obtain the relevant attributes of the file: file name, file type, and file size.
LabelUpResult. Text = "Upload successful! ";
LabelFileName. Text = "File Source:" + strName;
LabelFileExt. Text = "file type:" + inputFile. PostedFile. ContentType + "(" + strExt + ")";
LabelFileSize. Text = "file size:" + (inputFile. PostedFile. ContentLength/1024). ToString () + "K Byte (s )";
} Else {
LabelUpResult. Text = "select the file you want to upload! ";
LabelFileName. Text = "";
LabelFileExt. Text = "";
LabelFileSize. Text = "";
}
}
</Script>

<Html>
<Head>
<Title> upload a file </title>
</Head>
<Body>
<Div align = "center">

<Table width = "100%" border = "0" cellpadding = "0" cellspacing = "0" style = "border-collapse: collapse "bordercolor =" # eeeeee "id =" AutoNumber1 ">
<Form id = "formFile" method = "post" action = "" enctype = "multipart/form-data" runat = "server">
<Tr>
<Td align = "center">
<Input type = "file" id = "inputFile" name = "inputFile" runat = "server" size = "64">
</Td>
</Tr>
<Tr>
<Td align = "center">
<Input type = "button" value = "Upload" OnServerClick = "uploadFile" id = "Button1" name = "Button1" runat = "server">
</Td>
</Tr>
<Tr>
<Td align = "center">
<Asp: Label id = "labelUpResult" runat = "server" text = "" font-bold = "True" forecolor = "# FF0000"/> <br>
<Asp: Label id = "labelFileName" runat = "server" text = "" font-bold = "True" forecolor = "# FF0000"/> <br>
<Asp: Label id = "labelFileExt" runat = "server" text = "" font-bold = "True" forecolor = "# FF0000"/> <br>
<Asp: Label id = "labelFileSize" runat = "server" text = "" font-bold = "True" forecolor = "# FF0000"/> <br>
</Td>
</Tr>
</Form>
</Table>
</Div>
</Body>
</Html>

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.