注册 登录
编程论坛 ASP.NET技术论坛

同时上传多文件问题

ycbbg 发布于 2008-08-25 19:28, 1179 次点击
页面代码:<%@ 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 = "上传失败";
        }

    }
7 回复
#2
ycbbg2008-08-25 19:28
可上传不了,不知是出了什么问题
#3
bygg2008-08-26 10:46
不会是文件太大了吧?
#4
ycbbg2008-08-26 11:16
不是
#5
lxd8242008-08-27 11:35
出现什么提示?
#6
zklwzh2008-08-27 13:46
加上from表单试试.再不行,你就新建一个页面.然后再把相应代码放进去.
#7
ycbbg2008-08-28 11:14
还是没用,帮帮忙
#8
Xxibug2008-08-29 09:12
可以使用另一种方法,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]]
1