如何正确使用ASP.NET FileUpload控件

发布于 2024-08-21 05:40:03 字数 529 浏览 4 评论 0原文

我正在尝试在 ASP.NET 中使用 FileUpload 控件

这是我当前的命名空间设置:

using System;
using System.IO;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

在我的类中,我只是使用:

FileUpload fileUpload = new FileUpload();

但是,通常属于 FileUpload 一部分的属性似乎都不可用...例如作为.HasFile。我试图在后面的代码中创建按钮单击方法,我注意到 .HasFile 的大部分用法都在前面的代码中,但是我的理解是这应该不重要。

有谁知道为什么?

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.Generic;
using System.Linq;
using System.Text;

using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

And within my class, I'm just using:

FileUpload fileUpload = new FileUpload();

However, none of the attributes that are normally part of FileUpload seem to be available... such as .HasFile. I'm attempting to make the Button click method in the code behind, I have noticed that most of the usage of .HasFile is in the code in front, however it was my understanding that this shouldn't matter.

Does anyone know why?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

最初的梦 2024-08-28 05:40:03

ASP.NET 控件应该放置在 aspx 标记文件中。这是与他们合作的首选方式。因此,将 FileUpload 控件添加到您的页面。确保它具有所有必需的属性,包括 IDrunat

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

FileUpload1 的实例将在自动生成/更新的 *.designer 中自动创建。 cs 文件,它是您页面的部分类。您通常不必关心其中的内容,只需假设 aspx 页面上的任何控件都会自动实例化。

添加一个用于回发的按钮:

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

然后转到包含代码的 *.aspx.cs 文件并添加按钮单击处理程序。在 C# 中,它看起来像这样:

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

就是这样。一切都应该按预期工作。

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.

ヅ她的身影、若隐若现 2024-08-28 05:40:03

无需在代码隐藏文件中实例化 FileUpload,只需在标记文件(.aspx 文件)中声明它:

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

然后您将能够访问该控件的所有属性,例如 HasFile

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.

轮廓§ 2024-08-28 05:40:03

从后面的代码添加 FileUpload 控件应该可以正常工作,其中 HasFile 属性应该可用(例如在 Click 事件中)。

如果这些属性似乎不可用(无论是作为编译器错误还是通过智能感知),您可能正在引用与您想象的不同的变量。

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.

知足的幸福 2024-08-28 05:40:03

我在代码后面的解决方案是:

System.Web.UI.WebControls.FileUpload fileUpload;

我不知道为什么,但是当您在没有 System.Web.UI.WebControls 的情况下使用 FileUpload 时,它引用的不是 YourProject.FileUpload System.Web.UI.WebControls.FileUpload

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.

甜心小果奶 2024-08-28 05:40:03

我注意到,当智能对某个对象不起作用时,您正在处理的上面的类中通常会出现错误。

另一种选择是您没有将 FileUpload 对象实例化为实例变量。确保代码:

FileUpload fileUpload = new FileUpload();

不在后面代码的函数内。

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.

走野 2024-08-28 05:40:03

老问题,但仍然,如果它可能对某人有帮助,这里是

<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>

在代码隐藏中的完整示例,C# 代码用于抓取文件并将其保存在目录源中

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.";
    }

ASP.NET 中的文件上传(Web 表单上传控件示例)

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文