45fan.com - 路饭网

搜索: 您的位置主页 > 电脑频道 > 电脑教程 > 阅读资讯:如何使用html控件中file field实现WEB上传?

如何使用html控件中file field实现WEB上传?

2016-09-02 06:02:45 来源:www.45fan.com 【

如何使用html控件中file field实现WEB上传?

示例一:

protected void Button4_Click(object sender, EventArgs e)
{
string fullname = this.FileUpload4.PostedFile.FileName;
string filename = fullname.Substring(fullname.LastIndexOf("//") + 1);
string type = fullname.Substring(fullname.LastIndexOf(".")+1);
if (type == "jpg" || type == "gif" || type == "bmp")
{
this.FileUpload4.PostedFile.SaveAs(Server.MapPath("Upload") + "//" + filename);
}
else
{
Response.Write("<script language='java-script'>alert('你上传的文件格式不正确!')</script>");
}
}

说明:FileUpload为.NET中提供的控件;

示例二

在VS中先把html控件中file field拖放到页面中,然后选择file field点右键,选择”作为服务器控件运行“,就可以在.cs文件中对它进行控制了。

如何使用html控件中file field实现WEB上传?
UploadFile.aspx源程序

<%@ Page language="c#" Codebehind="UploadFile.aspx.cs" AutoEventWireup="false" Inherits="News.UploadFile" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>UploadFile</title>
<meta content="Microsoft Visual Studio 7.0" 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 encType="multipart/form-data" runat="server">
<TABLE id="Table1" style="Z-INDEX: 101; LEFT: 207px; WIDTH: 469px; POSITION: absolute; TOP: 109px; HEIGHT: 150px" cellSpacing="1" cellPadding="1" width="469" border="1">
<TR>
<TD>请选择要上传的文件</TD>
<TD>
<INPUT id="File1" type="file" name="File1" runat="server"></TD>
<TD></TD>
</TR>
<TR style="DISPLAY:none">
<TD>请输入文件名</TD>
<TD>
<asp:TextBox id="TextBox1" runat="server"></asp:TextBox></TD>
<TD></TD>
</TR>
<TR>
<TD></TD>
<TD>
<asp:Button id="Button1" runat="server" Text="上传" OnClick="ExecUpload"></asp:Button></TD>
<TD></TD>
</TR>
<tr>
<td><asp:Label id="Label1" style="Z-INDEX: 102; LEFT: 292px; POSITION: absolute; TOP: 333px" runat="server">Label</asp:Label></td>
<td><asp:Label id="Label2" style="Z-INDEX: 102; LEFT: 280px; POSITION: absolute; TOP: 296px" runat="server">Label</asp:Label></td>
</tr>
</TABLE>
</form>
</body>
</HTML>


UploadFile.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;
namespace News
{
/// <summary>
/// UploadFile 的摘要说明。
/// </summary>
public class UploadFile : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.HtmlControls.HtmlInputFile File1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Label Label1;

public void ExecUpload(object sender, System.EventArgs e)
{
if(File1.PostedFile!=null)
{
try
{
string newFile=Server.MapPath("pic_upload/")+File1.Value;
File1.PostedFile.SaveAs(newFile);

//上传保并可重新文件夹名File1.PostedFile.SaveAs(Request.PhysicalApplicationPath + "pic_upload" + File1.Value);
Label1.Text="完成上传至"+ newFile+ "<br>上传文件大小:"+File1.PostedFile.ContentLength + "字节";

Label2.Text="<a href='pic_upload/"+File1.PostedFile.FileName+"'>查看文件</a>";
}
catch(Exception ex)
{
if(ex!=null) Label1.Text=ex.ToString();
}

}
}

private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}



#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN:该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.Button1.Click += new System.EventHandler(this.ExecUpload);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

}
}

 

本文地址:http://www.45fan.com/dnjc/71060.html
Tags: 控件 File html
编辑:路饭网
关于我们 | 联系我们 | 友情链接 | 网站地图 | Sitemap | App | 返回顶部