c# 页面中想点击一个按钮然后弹出一个对话框来选择文件进行添加附件的操作,怎么做?

时间:2022-07-17 23:07:01
c# 页面中想点击一个按钮然后弹出一个对话框来选择文件进行添加附件的操作,怎么做?

6 个解决方案

#1


aspx:
<input type=file value=upload>
C#:
HttpFileCollection uploadFiles = HttpContext.Current.Request.Files;

#2


你是不是想将客户端的文件上传到服务器?

如果是的话,给你一段代码:

aspx文件:
================================
<%@ Page language="c#" Codebehind="WebForm3.aspx.cs" AutoEventWireup="false" Inherits="WebControlSample.WebForm3" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm3</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体"><INPUT style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" type="file" id="File1"
name="File1" runat="server"></FONT>
<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 168px; POSITION: absolute; TOP: 48px" runat="server"
Text="上传" Width="72px"></asp:Button>
<asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 88px; POSITION: absolute; TOP: 152px" runat="server"
Width="328px" Height="48px">Label</asp:Label>
</form>
</body>
</HTML>


其后台的类文件:
======================================
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 WebControlSample
{
/// <summary>
/// WebForm3 的摘要说明。
/// </summary>
public class WebForm3 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.HtmlControls.HtmlInputFile File1;

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

#region Web 窗体设计器生成的代码
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.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
string fileName=this.File1.PostedFile.FileName;
fileName=System.IO.Path.GetFileName(fileName);

(this.Request.ApplicationPath+@".\UpLoadFiles\"+fileName);
string uploadFile=this.Request.MapPath(this.Request.ApplicationPath)+@".\UpLoadFiles\"+fileName;


this.File1.PostedFile.SaveAs(@"c:\"+fileName);

}
}
}

#3


我也刚用JMAIL写了一个邮件发送程序,可发送附件
给你段代码:
1.添加附件页attpage.aspx:
<%@ Page Language="c#" ContentType="text/html" ResponseEncoding="gb2312" CodeBehind="attpage.aspx.cs" AutoEventWireup="false" Inherits="jmailsmtp.attpage" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<title>增删附件</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
<body>
<script language="javascript">
/*function isindrop(str,drop)
{
for(i=0;i<drop.length;i++)
{
if(str==drop.options[i].text)
return(true);
}
return(false);
}*/
function isindrop(str,drop)
{
for(i=0;i<drop.length;i++)
{
var tmp=drop.options[i].text;
if(str.substring(str.lastIndexOf("\\")+1)==tmp.substring(tmp.lastIndexOf("\\")+1))
{
/*set MyFileObject=Server.CreateObject("Scripting.FileSystemObject");
set mfile=MyFileObject.GetFile(str);
set mfile1=MyFileObject.GetFile(tmp);
if(mfile.size==mfile1.size)*/
return(true);
}
}
return(false);
}
function emptyfile(fbr)
{
fbr.select();
document.execCommand('Delete');
}
function madd(fbr)
{
if(isindrop(fbr.value,att))
{alert('您已经选择了这文件,请重新选择');
emptyfile(fbr);return;}
var oOption=document.createElement('OPTION');
oOption.text=fbr.value;
if(oOption.text!='')
att.add(oOption);
att.selectedIndex=att.length-1;
emptyfile(fbr);
}
</script>
<table width="120%" border="1">
<tr>
<td width="146">现有附件:</td>
<td><SELECT id="att" runat="server"></SELECT><INPUT id="delatt" type="button" value="删除" onclick="var idx=att.selectedIndex;if(idx==-1)return;att.remove(idx);if(idx>0) idx--;else if(att.length==0)idx=-1;att.selectedIndex=idx;"></td>
</tr>
<tr>
<td width="146">新增附件:</td>
<td><INPUT id="filebr" type="file"> <INPUT id="addatt" onclick="madd(filebr)" type="button" value="添加">
</td>
</tr>
</table>
<INPUT id="rtn" onclick="var mtxt='';for(i=0;i<att.length;i++){if(att.options[i].text!='')mtxt+=','+att.options[i].text;}window.opener.document.all['txtatt'].value=mtxt.substring(1);window.close();"
type="button" value="返回">
</body>
</HTML>
2.attpage.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 jmailsmtp
{
/// <summary>
/// attpage 的摘要说明。
/// </summary>
public class attpage : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlSelect att;

private void Page_Load(object sender, System.EventArgs e)
{
string sat=Request.QueryString["atts"];
if(sat==null)return;
if(sat.Trim()=="")
return;
string[] opt=sat.Split(',');
foreach(string ss in opt)
{
ListItem lst=new ListItem(ss);
if(ss.Trim()!="")
att.Items.Add(lst);
}
if(att.Items.Count>0)
att.SelectedIndex=0;
// 在此处放置用户代码以初始化页面
}

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

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

}
#endregion
}
}
3.在写信页的HTML中加入:
<td><INPUT id="txtatt" style="WIDTH: 280px; HEIGHT: 22px" type="text" size="41" runat="server"><INPUT id="attpage" type="button" value="增删附件" name="attpage" onclick="var ref=txtatt.value;window.open('attpage.aspx?atts='+ref);">
</td>

#4


<INPUT id="FileUp" onpropertychange="setValid()" type="file" name="FileUp" runat="server">

#5


up

#6


<input type=file value=upload>

#1


aspx:
<input type=file value=upload>
C#:
HttpFileCollection uploadFiles = HttpContext.Current.Request.Files;

#2


你是不是想将客户端的文件上传到服务器?

如果是的话,给你一段代码:

aspx文件:
================================
<%@ Page language="c#" Codebehind="WebForm3.aspx.cs" AutoEventWireup="false" Inherits="WebControlSample.WebForm3" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>WebForm3</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
</HEAD>
<body MS_POSITIONING="GridLayout">
<form id="Form1" method="post" runat="server">
<FONT face="宋体"><INPUT style="Z-INDEX: 101; LEFT: 8px; POSITION: absolute; TOP: 8px" type="file" id="File1"
name="File1" runat="server"></FONT>
<asp:Button id="Button1" style="Z-INDEX: 102; LEFT: 168px; POSITION: absolute; TOP: 48px" runat="server"
Text="上传" Width="72px"></asp:Button>
<asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 88px; POSITION: absolute; TOP: 152px" runat="server"
Width="328px" Height="48px">Label</asp:Label>
</form>
</body>
</HTML>


其后台的类文件:
======================================
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 WebControlSample
{
/// <summary>
/// WebForm3 的摘要说明。
/// </summary>
public class WebForm3 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.HtmlControls.HtmlInputFile File1;

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

#region Web 窗体设计器生成的代码
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.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion

private void Button1_Click(object sender, System.EventArgs e)
{
string fileName=this.File1.PostedFile.FileName;
fileName=System.IO.Path.GetFileName(fileName);

(this.Request.ApplicationPath+@".\UpLoadFiles\"+fileName);
string uploadFile=this.Request.MapPath(this.Request.ApplicationPath)+@".\UpLoadFiles\"+fileName;


this.File1.PostedFile.SaveAs(@"c:\"+fileName);

}
}
}

#3


我也刚用JMAIL写了一个邮件发送程序,可发送附件
给你段代码:
1.添加附件页attpage.aspx:
<%@ Page Language="c#" ContentType="text/html" ResponseEncoding="gb2312" CodeBehind="attpage.aspx.cs" AutoEventWireup="false" Inherits="jmailsmtp.attpage" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
<title>增删附件</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</HEAD>
<body>
<script language="javascript">
/*function isindrop(str,drop)
{
for(i=0;i<drop.length;i++)
{
if(str==drop.options[i].text)
return(true);
}
return(false);
}*/
function isindrop(str,drop)
{
for(i=0;i<drop.length;i++)
{
var tmp=drop.options[i].text;
if(str.substring(str.lastIndexOf("\\")+1)==tmp.substring(tmp.lastIndexOf("\\")+1))
{
/*set MyFileObject=Server.CreateObject("Scripting.FileSystemObject");
set mfile=MyFileObject.GetFile(str);
set mfile1=MyFileObject.GetFile(tmp);
if(mfile.size==mfile1.size)*/
return(true);
}
}
return(false);
}
function emptyfile(fbr)
{
fbr.select();
document.execCommand('Delete');
}
function madd(fbr)
{
if(isindrop(fbr.value,att))
{alert('您已经选择了这文件,请重新选择');
emptyfile(fbr);return;}
var oOption=document.createElement('OPTION');
oOption.text=fbr.value;
if(oOption.text!='')
att.add(oOption);
att.selectedIndex=att.length-1;
emptyfile(fbr);
}
</script>
<table width="120%" border="1">
<tr>
<td width="146">现有附件:</td>
<td><SELECT id="att" runat="server"></SELECT><INPUT id="delatt" type="button" value="删除" onclick="var idx=att.selectedIndex;if(idx==-1)return;att.remove(idx);if(idx>0) idx--;else if(att.length==0)idx=-1;att.selectedIndex=idx;"></td>
</tr>
<tr>
<td width="146">新增附件:</td>
<td><INPUT id="filebr" type="file"> <INPUT id="addatt" onclick="madd(filebr)" type="button" value="添加">
</td>
</tr>
</table>
<INPUT id="rtn" onclick="var mtxt='';for(i=0;i<att.length;i++){if(att.options[i].text!='')mtxt+=','+att.options[i].text;}window.opener.document.all['txtatt'].value=mtxt.substring(1);window.close();"
type="button" value="返回">
</body>
</HTML>
2.attpage.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 jmailsmtp
{
/// <summary>
/// attpage 的摘要说明。
/// </summary>
public class attpage : System.Web.UI.Page
{
protected System.Web.UI.HtmlControls.HtmlSelect att;

private void Page_Load(object sender, System.EventArgs e)
{
string sat=Request.QueryString["atts"];
if(sat==null)return;
if(sat.Trim()=="")
return;
string[] opt=sat.Split(',');
foreach(string ss in opt)
{
ListItem lst=new ListItem(ss);
if(ss.Trim()!="")
att.Items.Add(lst);
}
if(att.Items.Count>0)
att.SelectedIndex=0;
// 在此处放置用户代码以初始化页面
}

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

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

}
#endregion
}
}
3.在写信页的HTML中加入:
<td><INPUT id="txtatt" style="WIDTH: 280px; HEIGHT: 22px" type="text" size="41" runat="server"><INPUT id="attpage" type="button" value="增删附件" name="attpage" onclick="var ref=txtatt.value;window.open('attpage.aspx?atts='+ref);">
</td>

#4


<INPUT id="FileUp" onpropertychange="setValid()" type="file" name="FileUp" runat="server">

#5


up

#6


<input type=file value=upload>