How to correctly use the ASP.NET FileUpload control

后端 未结 6 1991
遥遥无期
遥遥无期 2020-12-09 03:47

I\'m trying to use the FileUpload control in ASP.NET

Here\'s my current namespace setup:

using System;
using System.IO;
using System.Collections.Gene         


        
相关标签:
6条回答
  • 2020-12-09 04:02

    Old Question, but still, if it might help someone, here is complete sample

    <form id="form1" runat="server">
        <div>
            <asp:FileUpload ID="FileUpload1" runat="server" /><br/>
            <asp:Button ID="Button1" runat="server" Text="Upload File" OnClick="UploadFile" /><br/>
            <asp:Label ID="Label1" runat="server" Text=""></asp:Label>
        </div>
    </form>
    

    In your Code-behind, C# code to grab file and save it in Directory

    protected void UploadFile(object sender, EventArgs e)
        {
            //folder path to save uploaded file
            string folderPath = Server.MapPath("~/Upload/");
    
            //Check whether Directory (Folder) exists, although we have created, if it si not created this code will check
            if (!Directory.Exists(folderPath))
            {
                //If folder does not exists. Create it.
                Directory.CreateDirectory(folderPath);
            }
    
           //save file in the specified folder and path
            FileUpload1.SaveAs(folderPath + Path.GetFileName(FileUpload1.FileName));
    
            //once file is uploaded show message to user in label control
            Label1.Text = Path.GetFileName(FileUpload1.FileName) + " has been uploaded.";
        }
    

    Source: File Upload in ASP.NET (Web-Forms Upload control example)

    0 讨论(0)
  • 2020-12-09 04:04

    Adding a FileUpload control from the code behind should work just fine, where the HasFile property should be available (for instance in your Click event).

    If the properties don't appear to be available (either as a compiler error or via intellisense), you probably are referencing a different variable than you think you are.

    0 讨论(0)
  • 2020-12-09 04:06

    ASP.NET controls should rather be placed in aspx markup file. That is the preferred way of working with them. So add FileUpload control to your page. Make sure it has all required attributes including ID and runat:

    <asp:FileUpload ID="FileUpload1" runat="server" />
    

    Instance of FileUpload1 will be automatically created in auto-generated/updated *.designer.cs file which is a partial class for your page. You usually do not have to care about what's in it, just assume that any control on an aspx page is automatically instantiated.

    Add a button that will do the post back:

    <asp:Button ID="Button1" runat="server" Text="Button" onclick="Button1_Click" />
    

    Then go to your *.aspx.cs file where you have your code and add button click handler. In C# it looks like this:

    protected void Button1_Click(object sender, EventArgs e)
    {
      if (this.FileUpload1.HasFile)
      {
        this.FileUpload1.SaveAs("c:\\" + this.FileUpload1.FileName);
      }
    }
    

    And that's it. All should work as expected.

    0 讨论(0)
  • 2020-12-09 04:13

    I have noticed that when intellisence doesn't work for an object there is usually an error somewhere in the class above line you are working on.

    The other option is that you didn't instantiated the FileUpload object as an instance variable. make sure the code:

    FileUpload fileUpload = new FileUpload();
    

    is not inside a function in your code behind.

    0 讨论(0)
  • 2020-12-09 04:17

    Instead of instantiating the FileUpload in your code behind file, just declare it in your markup file (.aspx file):

    <asp:FileUpload ID="fileUpload" runat="server" />
    

    Then you will be able to access all of the properties of the control, such as HasFile.

    0 讨论(0)
  • 2020-12-09 04:27

    My solution in code behind was:

    System.Web.UI.WebControls.FileUpload fileUpload;
    

    I don't know why, but when you are using FileUpload without System.Web.UI.WebControls it is referencing to YourProject.FileUpload not System.Web.UI.WebControls.FileUpload.

    0 讨论(0)
提交回复
热议问题