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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2241545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-07 00:40:31  来源:igfitidea点击:

How to correctly use the ASP.NET FileUpload control

c#asp.netnetworkingfile-upload

提问by Hosemeyer

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

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

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.

但是,通常属于 FileUpload 一部分的属性似乎都不可用……例如 .HasFile。我试图在后面的代码中创建 Button click 方法,我注意到 .HasFile 的大部分用法都在前面的代码中,但是我的理解是这应该无关紧要。

Does anyone know why?

有谁知道为什么?

采纳答案by Maciej

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

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

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

Instance of FileUpload1will 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.

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

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:

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

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.

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

回答by wsanville

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

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

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

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

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

回答by tbischel

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

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

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.

如果这些属性似乎不可用(作为编译器错误或通过智能感知),则您引用的变量可能与您认为的不同。

回答by Edison

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 fileUpload = new FileUpload();

is not inside a function in your code behind.

不在您的代码后面的函数内。

回答by Josef

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.WebControlsit is referencing to YourProject.FileUploadnot System.Web.UI.WebControls.FileUpload.

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