.net下軟件的自動升級--上傳
2024-07-10 12:59:07
供稿:網(wǎng)友
.net下軟件的自動升級--上傳
代碼如下:
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>
/// 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)
{
// 在此處放置用戶代碼以初始化頁面
if(labelerror.text != "")
{
labelerror.text = "";
}
}
#region web 窗體設(shè)計器生成的代碼
override protected void oninit(eventargs e)
{
//
// codegen: 該調(diào)用是 asp.net web 窗體設(shè)計器所必需的。
//
initializecomponent();
base.oninit(e);
}
/// <summary>
/// 設(shè)計器支持所需的方法 - 不要使用代碼編輯器修改
/// 此方法的內(nèi)容。
/// </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)
{
//檢查上傳文件不為空
if(myfile.postedfile.filename !="")
{
string fullname = myfile.postedfile.filename;
//得到這個文件的相關(guān)屬性:文件名,文件類型,文件大小
this.fname.text = myfile.postedfile.filename;
this.fenc.text = myfile.postedfile.contenttype;
this.fsize.text = myfile.postedfile.contentlength.tostring() + “ bytes“;
//取得文件名(抱括路徑)里最后一個"/"的索引
int j = fullname.lastindexof("//");
//取得文件名
string simplename = fullname.substring(j);
//保存文件到你所要的目錄,這里是iis根目錄下的uploadnote目錄.你可以改變.
//用server.mappath()取當前文件的絕對目錄.在asp.net里"/"為轉(zhuǎn)義字符,必須用"//"代替
string filepath = server.mappath("//uploadnote");
//myfile.postedfile.saveas("d://test//aa.doc");
myfile.postedfile.saveas(filepath + simplename);
// response.write(server.machinename);
}
else
{
labelerror.text = "請選擇要上傳的文件!";
}
}
}
}
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>文件上傳</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="400">
<tr>
<td><asp:label id="labelerror" runat="server"></asp:label>
</td>
</tr>
<tr>
<td>
<table width="564">
<tr>
<td>選擇要上傳的文件:</td>
</tr>
<tr>
<td><input id="myfile" type="file" name="myfile" runat="server" style="width: 480px; height: 22px"
size="60">
</td>
</tr>
<tr>
<td><input id="button1" type="button" value="上 傳" name="button1" runat="server"></td>
</tr>
</table>
<br>
<table width="564">
<tr>
<td width="200"><b>文件資料</b></td>
<td> </td>
</tr>
<tr>
<td>文件名 :</td>
<td><asp:label id="fname" runat="server" text=""></asp:label></td>
</tr>
<tr>
<td>文件類型 :</td>
<td><asp:label id="fenc" runat="server"></asp:label></td>
</tr>
<tr>
<td>文件大小 :</td>
<td><asp:label id="fsize" runat="server"></asp:label></td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</html>