当前位置:编程学习 > C#/ASP.NET >>

AspnetUpload.GetUploadFile()方法取不到页面上的file控件

 protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            string fpath = Path.Combine(Request.PhysicalApplicationPath, "UpFiles");
            if (!Directory.Exists(fpath))
              Directory.CreateDirectory(fpath);
            AspnetUpload upldr = new AspnetUpload();
            upldr.set_UploadFolder(fpath);
        }
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //将来两个路径结合组合成一个新的路径
        string fpath = Path.Combine(Request.PhysicalApplicationPath, "UpFiles");
        //获取界面上的所有上传组件
        UploadFileCollection files = AspnetUpload.GetUploadFiles("Files");
        //遍历上传组件
        if (files == null)
        {
            Response.Write("<script>window.alert(' 没有取到任何file控件! ')</script>");
        }
        foreach (UploadFile file in files)
        {
            if (file != null)
            {
                file.SaveAs(Path.Combine(fpath, Path.GetFileName(file.get_FileName())));
                label1.Text = "上传成功!";
                Response.Write("<script>window.alert(" + file.get_FileName() + ")</script>");
            }
            else 
            { 
                 Response.Write("<script>window.alert('未取到页面上的file控件')</script>");
            }
        }

调试的时候,显示到 UploadFileCollection files = AspnetUpload.GetUploadFiles("Files");的时候,files的Count是0,页面上放的是多个<input type="file" name="Files"> --------------------编程问答-------------------- 用Fileupload试试。还有的就是,为什么在页面上有那么多Input控件呢? --------------------编程问答-------------------- 没分了.................. --------------------编程问答--------------------
引用楼主 skybreakhaha 的回复:
files的Count是0,页面上放的是多个<input type="file" name="Files">

   <form runat="server" enctype="multipart/form-data">
    <input id="File1" name="File1" type="file" />
    <input id="File2" name="File2" type="file" />
    <input id="File3" name="File3" type="file" />
    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    </form>

    protected void Button1_Click(object sender, EventArgs e)
    {
        Response.Write(Request.Files.Count);
    }
//3
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,