Ajax file upload code

Source: Internet
Author: User

Ajax file upload code

<% @ Import Namespace = "System. IO" %>
<% @ Page Language = "C #" debug = "true" %>
<Html>
<Head>
<Title> ajax asp.net: uploads files. </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;
// 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 () + myFile. 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 "\"
// MyFile. PostedFile. SaveAs (Server. MapPath ("img/" + newname + newext ));
// MyFile. PostedFile. SaveAs (Server. MapPath ("img/" + ));
// 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 ();
// Fsize. Text = System. IO. Path. GetFileName (nam );

String localname = System. IO. Path. GetFileName (myFile. Value );
String path = myFile. PostedFile. FileName;
MyFile. PostedFile. SaveAs (Server. MapPath ("img/" + localname ));
Fname. Text = localname;
Fenc. Text = System. IO. Path. GetExtension (path );
Filepath. Text = path;
Filebytes. Text = myFile. PostedFile. ContentLength. ToString ();
}
}


</Script>
</Head>
<Body>
<Center>
<H3> File Upload instance, from <a href = "http://www.chinabs.net"> BS Network </a> <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 path: </td>
<Td> <asp: label id = "filepath" runat = "server"/> </td> </tr>
<Tr>
<Td> file size: </td>
<Td> <asp: label id = "filebytes" runat = "server"/> </td> </tr>
</Table>

<Br>
<Br>
<Br>
</Center>
</Body>
</Html>

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.