问:
如何做: WEB多文件同时选择后逐一上传控件?
谢谢!~
______________________________________________________________________________________________
答1:
http://dotnet.aspx.cc/ShowDetail.aspx?id=58EA3515-36F2-4FD9-AC89-EAF49F59816C
______________________________________________________________________________________________
答2:
TO :net_lover(孟子E章) :
不用HTML的控件,因为它不能一次选择多个文件,只能一个一个点击,上传多个文件时太麻烦了。我想一次可以框选择多个文件,然后上传,如何实现?
______________________________________________________________________________________________
答3:
给你例子看看:
//upload.aspx
<%@ Page language="c#" Codebehind="upload.aspx.cs" AutoEventWireup="false" Inherits="RiseGuide.MegaNet.admin.upload" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>upload</title>
<meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
<meta name="CODE_LANGUAGE" Content="C#">
<meta name="vs_defaultClientScript" content="JavaScript">
<LINK href="../cmdjs/wgnet_style.css" type="text/css" rel="stylesheet">
<meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
<script id="clientEventHandlersJS" language="javascript">
<!--
var i;
function File_onblur(e) {
try{
i=e.name.replace("File","");
var path=document.all["File"+i.toString()].value;
document.all["Text"+i.toString()].value=getname(path);
}catch(e){
}
}
function getname(path){
var f1;
var name;
f1=path.split("\\");
name=f1[f1.length-1];
return name;
}
function Text_onfocus(e) {
try{
i=e.name.replace("Text","");
var path=document.all["File"+i.toString()].value;
if(document.all["Text"+i.toString()].value=="")
document.all["Text"+i.toString()].value=getname(path);
}catch(e){
}
}
//-->
</script>
</HEAD>
<body>
<FORM id="Form1" method="post" encType="multipart/form-data" runat="server">
<FONT face="宋体"> 文件上传系统
<asp:TextBox id="TextBox1" runat="server" Width="24px" ToolTip="改变文本框的值可以增加上传文件个数.." MaxLength="2"
AutoPostBack="True">1</asp:TextBox> 上传文件个数
<TABLE id="TableMain" height="82" cellSpacing="0" cellPadding="4" width="500" border="1"
runat="server" style="BORDER-COLLAPSE: collapse">
<TR>
<TD width="203" height="28">文件名称(默认为文件名)</TD>
<TD height="28">选择上传的文件</TD>
</TR>
<TR>
<TD align="right" colSpan="2">
<asp:Button id="Button1" runat="server" Text=" 保存 " Width="72px"></asp:Button>
</TD>
</TR>
</TABLE>
</FONT>
<asp:Label id="Label1" runat="server" Width="500px">注意:文件总大小不能超过4M..</asp:Label>
</FORM>
</body>
</HTML>
______________________________________________________________________________________________
答4:
//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;
using RiseGuideClassLibrary;
namespace RiseGuide.MegaNet.admin
{
/// <summary>
/// upload 的摘要说明。
/// </summary>
public class upload : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.HtmlControls.HtmlTable TableMain;
int FileCount=1;
private void Page_Load(object sender, System.EventArgs e)
{
Response.Clear();
this.Label1.Text ="提示:上传文件大小总和不能超过4M..";
try
{
FileCount=Int32.Parse(this.TextBox1.Text);
}
catch
{
this.TextBox1.Text="1";
FileCount=1;
}
if(!IsPostBack)
{
CreateInputFile();
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.TextBox1.TextChanged += new System.EventHandler(this.TextBox1_TextChanged);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
/// <summary>
/// 创建文件选择框
/// </summary>
private void CreateInputFile()
{
while(TableMain.Rows.Count >2)
{
TableMain.Rows.RemoveAt(TableMain.Rows.Count-1);//清除代码产生的行
}
for(int i=0;i<FileCount;i++)
{
int pos=TableMain.Rows.Count-1; //倒数第二行
HtmlTableRow row=new HtmlTableRow();
HtmlTableCell cell1=new HtmlTableCell();
HtmlInputText txt=new HtmlInputText();
txt.ID = "Text"+(i+1).ToString();
txt.Attributes["onfocus"]="javascript:Text_onfocus(this)";
cell1.Controls.Add(txt);
HtmlTableCell cell2=new HtmlTableCell();
HtmlInputFile file=new HtmlInputFile();
file.ID= "File"+(i+1).ToString();
txt.Attributes["onblur"]="javascript:File_onblur(this)";
cell2.Controls.Add(file);
row.Cells.Add(cell1);
row.Cells.Add(cell2);
//插入生成的行
this.TableMain.Rows.Insert(pos,row);
}
//this.Label1.Text ="提示:当前创建了"+this.TextBox1.Text+"个文件上传框";
}
private void Button1_Click(object sender, System.EventArgs e)
{
HttpFileCollection f=Request.Files;
this.Label1.Text ="提示:";
if(Request.TotalBytes>4096000) //判断输入大小
{
this.Label1.Text ="提示:上传文件内容总和大于了4M,请重新选择文件";
CreateInputFile();
return ;
}
for(int i=0;i<f.Count ;i++)
{
if(Request.Form["File"+(i+1).ToString()]=="")
continue;
if(f[i].FileName =="")
{
this.Label1.Text ="提示:请选择您要上传的文件";
continue;
}
string name="";
try
{
string[] names=f[i].FileName.Split('\\');
name=names[names.Length-1];
f[i].SaveAs(Server.MapPath("../photo")+"\\"+name);
if(Request.Form["Text"+(i+1).ToString()]=="")
SysPhoto(name.Split('.')[0]+DateTime.Now.ToString(),name);
else
SysPhoto(Request.Form["Text"+(i+1).ToString()],name);
this.Label1.Text += f[i].FileName +" 上传 成功..<BR>";
}
#if DEBUG
catch(Exception err)
{
this.Label1.Text += f[i].FileName +" 上传 失败.."+err.Message+"<BR>";
}
#else
catch
{
this.Label1.Text += f[i].FileName +" 上传 失败..<BR>";
}
#endif
}
CreateInputFile();
}
private void TextBox1_TextChanged(object sender, System.EventArgs e)
{
CreateInputFile();
}
/// <summary>
/// 保存到数据库
/// </summary>
private void SysPhoto(string name,string url)
{
dataBase db=new dataBase();
db.ExecuteSql("insert into Sysphoto (name,url) values ('"+name+"','"+url+"')");
}
}
}