How to correctly use the ASP.NET FileUpload control

后端 未结 6 2009
遥遥无期
遥遥无期 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: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:

    
    

    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:

    
    

    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.

提交回复
热议问题