| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1158 人关注过本帖
标题:同时上传多文件问题
只看楼主 加入收藏
ycbbg
Rank: 1
来 自:江西
等 级:新手上路
帖 子:99
专家分:0
注 册:2007-11-26
结帖率:100%
收藏
 问题点数:0 回复次数:7 
同时上传多文件问题
页面代码:<%@ Control Language="C#" AutoEventWireup="true" CodeFile="uploadmorefile.ascx.cs" Inherits="controls_upload" %>
 <script type="text/javascript">
    function addFileControl()
    {
        var str = '<br/><INPUT type="file" NAME="File" id="File">'
        document.getElementById('FileCollection').insertAdjacentHTML("beforeEnd",str)
    }
    </script>

   
        <table width="500" border="0" cellpadding="0" cellspacing="0" align="center">
            <tr>
                <td height="25" colspan="3">
                    &nbsp;</td>
            </tr>
            <tr>
                <td height="25" colspan="3">
                    <P id="FileCollection"><INPUT type="file" name="File" id="File">
                        <asp:Label ID="Label1" runat="server" ForeColor="Red"></asp:Label></P></td>
            </tr>
            <tr>
                <td width="200" height="25">
                    <input name="button" type="button" onclick="addFileControl()" value="增加(File)"></td>
                <td width="200">
                    <asp:Button ID="Upload" runat="server" Text="上传" Width="110px" OnClick="Upload_Click">
                    </asp:Button></td>
                <td width="200">
                    <input name="button2" type="button" style="width: 56px; height: 24px" onclick="this.form.reset()"
                        value="重置"></td>
            </tr>
            <tr>
                <td height="25" colspan="3">
                    <asp:Label ID="strStatus" runat="server" BorderColor="White" BorderStyle="None" Width="500px"
                        Font-Size="9pt" Font-Bold="True" Font-Names="宋体"></asp:Label></td>
            </tr>
        </table>
  


CS代码:

 private bool upMorefile()
    {
        //遍历File表单元素
        System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
        //状态信息
        System.Text.StringBuilder strMsg = new System.Text.StringBuilder("上传的文件信息分别为:<hr color=red>");
        int fileCount;
        int filecount = files.Count;
        try
        {
            for (fileCount = 0; fileCount < files.Count; fileCount++)
            {
                //定义访问客户端上传文件的对象
                System.Web.HttpPostedFile postedFile = files[fileCount];
                string fileName, fileExtension;
                //取得上传得文件名
                fileName = (postedFile.FileName);
                if (fileName != String.Empty)
                {
                    //取得文件的扩展名
                    fileExtension = (fileName);
                    //上传的文件信息
                    strMsg.Append("上传的文件类型:" + postedFile.ContentType.ToString() + "<br>");
                    strMsg.Append("客户端文件地址:" + postedFile.FileName + "<br>");
                    strMsg.Append("上传文件的文件名:" + fileName + "<br>");
                    strMsg.Append("上传文件的扩展名:" + fileExtension + "<br><hr color=red>");
                    //保存到指定的文件夹
                    postedFile.SaveAs(Server.MapPath("map/") + fileName);
                }
            }
            strStatus.Text = strMsg.ToString();
            return true;
        }
        catch (System.Exception error)
        {
            strStatus.Text = error.Message;
            return false;

        }
    }
    protected void Upload_Click(object sender, EventArgs e)
    {
        if (upMorefile())
        {
            Label1.Text = "上传成功";
        }
        else
        {
            Label1.Text = "上传失败";
        }

    }
搜索更多相关主题的帖子: 同时上传多文件问题 
2008-08-25 19:28
ycbbg
Rank: 1
来 自:江西
等 级:新手上路
帖 子:99
专家分:0
注 册:2007-11-26
收藏
得分:0 
可上传不了,不知是出了什么问题

http://www.
2008-08-25 19:28
bygg
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:乖乖的心中
等 级:版主
威 望:241
帖 子:13555
专家分:3076
注 册:2006-10-23
收藏
得分:0 
不会是文件太大了吧?

飘过~~
2008-08-26 10:46
ycbbg
Rank: 1
来 自:江西
等 级:新手上路
帖 子:99
专家分:0
注 册:2007-11-26
收藏
得分:0 
不是

http://www.
2008-08-26 11:16
lxd824
Rank: 1
等 级:新手上路
帖 子:135
专家分:0
注 册:2007-4-21
收藏
得分:0 
出现什么提示?
2008-08-27 11:35
zklwzh
Rank: 2
等 级:论坛游民
帖 子:275
专家分:25
注 册:2005-11-16
收藏
得分:0 
加上from表单试试.再不行,你就新建一个页面.然后再把相应代码放进去.

2008-08-27 13:46
ycbbg
Rank: 1
来 自:江西
等 级:新手上路
帖 子:99
专家分:0
注 册:2007-11-26
收藏
得分:0 
还是没用,帮帮忙

http://www.
2008-08-28 11:14
Xxibug
Rank: 1
等 级:新手上路
威 望:1
帖 子:95
专家分:0
注 册:2006-1-31
收藏
得分:0 
可以使用另一种方法,Request有一个Files的方法,返回的是一个HttpFileCollection的集合,其集合的元素就是HttpPostedFile,所以有这样的方式
int countFiles = Request.Files.Count;
for(int i = 0; i < countFiles; i++)
{
    HttpPostedFile hpf = Request.Files[i];
    .....
}在上面判断路径的时候要注意""的内容,即有些上传不选,如果不加判断会出现上传能上传但页面会出现路径找不到的问题,这里可以使用一个方式,如:
if(hpf.ContentLength != 0)来判断,ContentLength代表着上传内容的大小,以字节计,具体的可以参考MSDN

在html元素内form的html控件必须带Enctype = "multipart/form-data",否则会出现上传不完整
如:
<form id = "myForm" runat = "server" Enctype = ""multipart/form-data">
<input type = "file" id = "fileUpload1" runat = "server" />
<input type = "file" id = "fileUpload2" runat = "server" />
<input type = "file" id = "fileUpload3" runat = "server" />
.......



[[it] 本帖最后由 Xxibug 于 2008-8-29 09:13 编辑 [/it]]

=======【天上有人 地上有狼】=======
2008-08-29 09:12
快速回复:同时上传多文件问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.020035 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved