Automatic upgrade of software under. Net--upload

Source: Internet
Author: User
Tags file size file upload root directory visual studio
Upload the automatic upgrade of the software under. Net--upload

The code is as follows:


UpLoad.aspx.cs

Using System;
Using System.Collections;
Using System.ComponentModel;
Using System.Data;
Using System.Drawing;
Using System.Web;
Using System.Web.SessionState;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.Web.UI.HtmlControls;
Using System.IO;

Namespace Uploadnote
{
<summary>
Summary description of the WebForm1.
</summary>
public class UpLoad:System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label fname;
protected System.Web.UI.WebControls.Label Fenc;
protected System.Web.UI.WebControls.Label fsize;
protected System.Web.UI.WebControls.Label Labelerror;
protected System.Web.UI.HtmlControls.HtmlInputFile myFile;
protected System.Web.UI.HtmlControls.HtmlInputButton Button1;

private void Page_Load (object sender, System.EventArgs e)
{
Place user code here to initialize page
if (Labelerror.text!= "")
{
Labelerror.text = "";
}
}
Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
//
CodeGen: This call is required for the ASP.net Web forms Designer.
//
InitializeComponent ();
Base. OnInit (e);
}

<summary>
Designer supports required methods-do not use the Code editor to modify
The contents of this method.
</summary>
private void InitializeComponent ()
{
This. Button1.serverclick + = new System.EventHandler (this. Button1_serverclick);
This. Load + = new System.EventHandler (this. Page_Load);

}
#endregion

private void Button1_serverclick (object sender, System.EventArgs e)
{
Check that the upload file is not empty
if (MyFile.PostedFile.FileName!= "")
{
string FullName = MyFile.PostedFile.FileName;

Get the related attributes of this file: file name, file type, file size
This.fname.Text = MyFile.PostedFile.FileName;
This.fenc.Text = MyFile.PostedFile.ContentType;
This.fsize.Text = myFile.PostedFile.ContentLength.ToString () + "bytes";

Gets the index of the last "\" in the filename (enclosing path)
Int J = fullname.lastindexof ("\ \");
Get file name
String simplename = Fullname.substring (j);

Save the file to the directory you want, this is the Uploadnote directory under the IIS root directory. You can change.
Use Server.MapPath () to take the absolute directory of the current file. In asp.net, "\" is an escape character and must be replaced with "\"
String FilePath = Server.MapPath ("\\UpLoadNote");
MyFile.PostedFile.SaveAs ("D:\\test\\aa.doc");
MyFile.PostedFile.SaveAs (FilePath + simplename);
Response.Write (Server.machinename);
}
Else
{
Labelerror.text = "Please select the file to upload!" ";
}
}
}
}


Upload.aspx

<%@ Page language= "C #" debug= "true" codebehind= "UpLoad.aspx.cs" autoeventwireup= "false" inherits= " Uploadnote.upload "%>
<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 transitional//en" >
<HTML>
<HEAD>
<title> File Upload </title>
<meta content= "Microsoft Visual Studio. NET 7.1" name= "generator" >
<meta content= "C #" Name= "Code_language" >
<meta content= "JavaScript" name= "vs_defaultClientScript" >
<meta content= "http://schemas.microsoft.com/intellisense/ie5" name= "Vs_targetschema" >
</HEAD>
<body ms_positioning= "GridLayout" >
<form id= "Uploderform" action= "upload.aspx" method= "POST" runat= "Server" >
<table cellspacing= "2" cellpadding= "2" align= "Center" border= "1" width= "580" height= ">"
<tr>
<td><asp:label id= "Labelerror" runat= "Server" ></asp:Label>
</td>
</tr>
<tr>
<td>
<table width= "564" >
<tr>
<td> Select the file to upload:</td>
</tr>
<tr>
<td><input id= "MyFile" type= "file" name= "MyFile" "runat=" Server "style=" width:480px; Height:22px "
Size= ">"
</td>
</tr>
<tr>
<td><input id= "Button1" type= "button" value= "Upload" name= "Button1" runat= "Server" ></td>
</tr>
</table>
<br>
<table width= "564" >
<tr>
&LT;TD width= ><b> Documents </b></td>
<td> </td>
</tr>
<tr>
<td> filename:</td>
<td><asp:label id= "fname" runat= "Server" text= "" ></asp:label></td>
</tr>
<tr>
<td> File Type:</td>
<td><asp:label id= "Fenc" runat= "Server" ></asp:label></td>
</tr>
<tr>
<td> File Size:</td>
<td><asp:label id= "fsize" runat= "Server" ></asp:label></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</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.