none
fileuploadmulti的问题 RRS feed

  • 问题

  • fileuploadmulti.aspx
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="FileUploadMulti.aspx.cs" Inherits="FileUploadMulti" %>

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml" >
    <head runat="server">
        <title>未命名页面</title>
    </head>
    <body>
        <form id="form1" runat="server">
        <div>
            <table>
                <tr style="background-color: #ffccff">
                    <td colspan="2" style="height: 26px; text-align: center">
                        请指定文件<asp:Label ID="txtFileSize" runat="server"></asp:Label></td>
                </tr>
                <tr style="background-color: #ccff99;">
                    <td style="width: 100px; text-align: center; height: 26px;">
                        01</td>
                    <td style="width: 100px; height: 26px;">
            <asp:FileUpload ID="FileUpload1" runat="server" Width="400px" /></td>
                </tr>
                <tr style="background-color: lightcyan;">
                    <td style="width: 100px; text-align: center">
                        02</td>
                    <td style="width: 100px">
                        <asp:FileUpload ID="FileUpload2" runat="server" Width="400px" /></td>
                </tr>
                <tr style="background-color: #ccff99;">
                    <td style="width: 100px; text-align: center">
                        03</td>
                    <td style="width: 100px">
                        <asp:FileUpload ID="FileUpload3" runat="server" Width="400px" /></td>
                </tr>
                <tr style="background-color: lightcyan;">
                    <td style="width: 100px; text-align: center; height: 26px;">
                        04</td>
                    <td style="width: 100px; height: 26px;">
                        <asp:FileUpload ID="FileUpload4" runat="server" Width="400px" /></td>
                </tr>
                <tr style="background-color: #ccff99;">
                    <td style="width: 100px; text-align: center">
                        05</td>
                    <td style="width: 100px">
                        <asp:FileUpload ID="FileUpload5" runat="server" Width="400px" /></td>
                </tr>
                <tr style="background-color: lightcyan;">
                    <td style="width: 100px; text-align: center">
                        06</td>
                    <td style="width: 100px">
                        <asp:FileUpload ID="FileUpload6" runat="server" Width="400px" /></td>
                </tr>
                <tr style="background-color: #ccff99;">
                    <td style="width: 100px; text-align: center">
                        07</td>
                    <td style="width: 100px">
                        <asp:FileUpload ID="FileUpload7" runat="server" Width="400px" /></td>
                </tr>
                <tr style="background-color: lightcyan;">
                    <td style="width: 100px; text-align: center">
                        08</td>
                    <td style="width: 100px">
                        <asp:FileUpload ID="FileUpload8" runat="server" Width="400px" /></td>
                </tr>
                <tr style="background-color: #ccff99;">
                    <td style="width: 100px; text-align: center">
                        09</td>
                    <td style="width: 100px">
                        <asp:FileUpload ID="FileUpload9" runat="server" Width="400px" /></td>
                </tr>
                <tr style="background-color: lightcyan;">
                    <td style="width: 100px; text-align: center">
                        10</td>
                    <td style="width: 100px; height: 26px;">
                        <asp:FileUpload ID="FileUpload10" runat="server" Width="400px" /></td>
                </tr>
            </table>
            <asp:Button ID="btnUpload" runat="server" Text="文件上传" OnClick="btnUpload_Click" />
            <br />
            <br />
            <asp:Label ID="txtMsg" runat="server"></asp:Label><br />
            &nbsp;</div>
        </form>
    </body>
    </html>


    fileuploadmulti.aspx.cs
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    using System.Web.Configuration;

    public partial class FileUploadMulti : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
      if (!IsPostBack)
      {
         Configuration config = WebConfigurationManager.OpenWebConfiguration(this.Request.ApplicationPath);
     
       HttpRuntimeSection myHttpRuntimeSection = (HttpRuntimeSection)config.GetSection("system.web/httpRuntime");
       
       txtFileSize.Text = "(文件上传总和最大限制为" + string.Format("{0:N0}", Convert.ToInt32(myHttpRuntimeSection.MaxRequestLength)) + "KB大小)";
       FileUpload1.Focus();
      }
        }

     protected void btnUpload_Click(object sender, EventArgs e)
     {
      txtMsg.Text = "";
      try
      {
       Upload(FileUpload1); //报错,与“FileUploadMulti.Upload(FileUpload)”最匹配的重载方法具有一些无效参数.咋办?
    //参数“1”: 无法从“System.Web.UI.WebControls.FileUpload”转换为“FileUpload”? 下面都有同样的报错。
       Upload(FileUpload2);
       Upload(FileUpload3);
       Upload(FileUpload4);
       Upload(FileUpload5);
       Upload(FileUpload6);
       Upload(FileUpload7);
       Upload(FileUpload8);
       Upload(FileUpload9);
       Upload(FileUpload10);

       txtMsg.Text = "完成文件上传如下:<BR>" + ViewState["Uploads"].ToString();
       ViewState["Uploads"] = null;
      }
      catch (Exception ex)
      {
       txtMsg.Text = ex.Message;
      }
     }


     private void Upload(FileUpload myFileUpload)
     {
      
      string path = HttpContext.Current.Request.MapPath("~/");
     
      if (myFileUpload.HasFile)  ///“FileUpload”并不包含“HasFile”的定义?又报错了,咋办?
      {
       try
       {

        myFileUpload.SaveAs(path + myFileUpload.FileName); //“FileUpload”并不包含“SaveAs”的定义?这里也报错 FileUpload”并不包含“FileName”的定义
        ViewState["Uploads"] += myFileUpload.PostedFile.FileName + "<br>";//FileUpload”并不包含“PostedFile”的定义
       }
       catch (Exception ex)
       {
        txtMsg.Text += ex.Message;
       }
      }
      else
      {

       if (hasFile())
       {
        txtMsg.Text = "必须指定文件!";
       }
      }
     }


     private bool hasFile()
     {
     
      bool status = false;

      status = FileUpload1.HasFile || FileUpload2.HasFile || FileUpload3.HasFile || FileUpload4.HasFile || FileUpload5.HasFile || FileUpload6.HasFile || FileUpload7.HasFile || FileUpload8.HasFile || FileUpload9.HasFile || FileUpload10.HasFile;

      /*以上写法等同下面写法
      status = FileUpload1.HasFile;
      status = FileUpload2.HasFile;
      status = FileUpload3.HasFile;
      status = FileUpload4.HasFile;
      status = FileUpload5.HasFile;
      status = FileUpload6.HasFile;
      status = FileUpload7.HasFile;
      status = FileUpload8.HasFile;
      status = FileUpload9.HasFile;
      status = FileUpload10.HasFile;
       * */
      return status;
     }
    }


    maiyude
    2009年8月23日 7:57

答案

  • 你好请检查下你的网站的命名空间下是否有类名叫 FileUpload 的 如果有将其更改为其他名称,一般不要与.NET中已有的类重复
    其次你可以用 private void Upload(System.Web.UI.WebControls.FileUpload myFileUpload)
     {
    
    ...
    
    替代原来的方法试试
    

    Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!http://hi.baidu.com/1987raymond
    2009年8月23日 8:09
    版主

全部回复