web 上传多个文件

winnerfat 2010-07-18 09:54:00

int i = 1;
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
SFUPC();
}
}
private void SFUPC()//该方法用于保存当前页面上传文件控件集到缓存中
{
ArrayList AL = new ArrayList();//创建动态增加数组


foreach (Control C in F.Controls)
{

if (C.GetType().ToString() == "System.Web.UI.HtmlControls.HtmlTableRow")
{
HtmlTableCell HTC = (HtmlTableCell)C.Controls[0];
foreach (Control FUC in HTC.Controls)
{
if (FUC.GetType().ToString() == "System.Web.UI.WebControls.FileUpload")
{
System.Web.UI.WebControls.FileUpload FU = (System.Web.UI.WebControls.FileUpload)FUC;
AL.Add(FU);
}
}
}
}
Session.Add("FilesControls", AL);
}
private void InsertC()//该方法用于添加一个上传文件的控件
{
ArrayList AL = new ArrayList();

this.F.Rows.Clear();
GetInfo();
HtmlTableRow HTR = new HtmlTableRow();
HtmlTableCell HTC = new HtmlTableCell();

HTC.Controls.Add(new FileUpload());
HTR.Controls.Add(HTC);
this.F.Controls.Add(HTR);
SFUPC();
i = F.Rows.Count;


}
private void GetInfo()//该方法用于读取缓存中存储的上传文件控件集
{
ArrayList AL = new ArrayList();
if (Session["FilesControls"] != null)
{
AL = (System.Collections.ArrayList)Session["FilesControls"];
for (int i = 0; i < AL.Count; i++)
{
HtmlTableRow HTR = new HtmlTableRow();
HtmlTableCell HTC = new HtmlTableCell();
HTC.Controls.Add((System.Web.UI.WebControls.FileUpload)AL[i]);
HTR.Controls.Add(HTC);
this.F.Rows.Add(HTR);
}
}
}
private void UpFile()//该方法用于执行文件上传操作
{
string FilePath = Server.MapPath("./") + "File";

HttpFileCollection HFC = Request.Files;

for (int i = 0; i < HFC.Count; i++)
{

HttpPostedFile UserHPF = HFC[i];
try
{
if (UserHPF.ContentLength > 0)
{

UserHPF.SaveAs(FilePath +"\\" + System.IO.Path.GetFileName(UserHPF.FileName));
}
}
catch
{
lbl_message.Text = "处理出错!";
}
}
if (Session["FilesControls"] != null)
{
Session.Remove("FilesControls");
}
lbl_message.Text = "上传成功!";

}


上传调用 UpFile 函数 添加一项 InsertC函数

我这段代码有什么错吗? 编译过去了。上传也好使。。。 不过“添加一项” 就不好使。。不能实现多个上传。。只能要我原来在视图拖进去的那个FileUpload控件。。。。

麻烦帮看一下这代码、、、、、、、、、我找了一天都找不出来问题所在、、、、、
想谢谢
...全文
186 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
托尔斯泰.NET 2010-07-20
  • 打赏
  • 举报
回复

js :code
<script language="JavaScript" type="text/javascript">
function addFile() {
var str = "<br/><input type='file' style='width:300px' name='File' />"
document.getElementById('PnImage').insertAdjacentHTML("beforeEnd", str)
}
</script>



html:Code

<asp:Panel ID="PnImage" runat="server">
<asp:FileUpload ID="fileupload" runat="server" Width="300px" />
</asp:Panel>
<asp:Label ID="lblPicture" runat="server" Text="(默认情况下图片:qq.gif)<a href='javascript:void(0)' onclick='addFile()'>添加一张</a> "></asp:Label>





后台代码:

//开始上传
protected void btnSave_Click(object sender, EventArgs e)
{
try
{
HttpFileCollection files = HttpContext.Current.Request.Files;
for (int iFile = 0; iFile < files.Count; iFile++)
{
///'检查文件扩展名字
HttpPostedFile postedFile = files[iFile];
string strPath = postedFile.FileName;
int index = strPath.LastIndexOf(".");
int indexc = strPath.LastIndexOf("\\");
//获取上传文件扩展名
string strExtends = strPath.Substring(index);
//32位Guid,不可能一样的值
string strName = strPath.Substring(indexc);
this.fileupload.PostedFile.SaveAs(Server.MapPath("../Upload/FCKEditor/image/") + strName);
//存入数据库相对路径
//model.Photos = strName;
// Response.Write(postedFile.FileName);
postedFile.SaveAs(Server.MapPath("../Upload/FCKEditor/image/") + strName);

}
Response.Write("<script>alert('操作成功')</script>");
// ClientScript.RegisterStartupScript(typeof(string), "hash", "$.prompt('操作成功',{opacity: 0.2})", true);
}
catch (Exception ex)
{

Response.Write("<script>alert(" + ex.Message + ")</script>");
}
}




按照此例子 做 可成功。。、、
shenrujun 2010-07-20
  • 打赏
  • 举报
回复
学习 一下
slcy147 2010-07-20
  • 打赏
  • 举报
回复
你到网上下载个SwfuploadDemo控件,挺好用的,可以批量上传图片,还可以显示进度。支持web开发
winnerfat 2010-07-19
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 wuyq11 的回复:]

function AddFile()
{
var strFile="<input name=\"upload_A\" type='file' class='inputText' style='WIDTH:350px;'/><br/>";
document.getElementById("td_uploadFile").insertAdjacentHTML("beforeEnd",strFi……
[/Quote]


谢谢!
为什么这段代码单独拿出来做一个项目。。。它就能出现多个上传文件的框。。。。。。。。。。。
而把它放到一个继承于模板页的一个网页里面 按出现上传文件 就永远是一个框呢
wuyq11 2010-07-18
  • 打赏
  • 举报
回复
function AddFile()
{
var strFile="<input name=\"upload_A\" type='file' class='inputText' style='WIDTH:350px;'/><br/>";
document.getElementById("td_uploadFile").insertAdjacentHTML("beforeEnd",strFile);
}
<td id="td_uploadFile" align="center">
<input id="upload_A" name="upload_A" type="file" class="inputText"
style="width: 350px; display:none;" runat="server" />
</td>

HttpFileCollection Files = HttpContext.Current.Request.Files;
for (int i = 0; i < Files.Count; i++)
{

HttpPostedFile PostedFile = Files[i];
if (PostedFile.ContentLength > 0)
{
string FileName = PostedFile.FileName;
string AttachExt = FileName.Substring(FileName.LastIndexOf(".") + 1);
int TotalSize = PostedFile.ContentLength;
}
}
或使用控件http://www.open-open.com/ajax/Upload.htm

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧