如何正确使用ASP.NET FileUpload控件
我正在尝试在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
ASP.NET 控件应该放置在 aspx 标记文件中。这是与他们合作的首选方式。因此,将
FileUpload
控件添加到您的页面。确保它具有所有必需的属性,包括ID
和runat
:FileUpload1
的实例将在自动生成/更新的 *.designer 中自动创建。 cs 文件,它是您页面的部分类。您通常不必关心其中的内容,只需假设 aspx 页面上的任何控件都会自动实例化。添加一个用于回发的按钮:
然后转到包含代码的 *.aspx.cs 文件并添加按钮单击处理程序。在 C# 中,它看起来像这样:
就是这样。一切都应该按预期工作。
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 includingID
andrunat
: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:
And that's it. All should work as expected.
无需在代码隐藏文件中实例化
FileUpload
,只需在标记文件(.aspx 文件)中声明它:然后您将能够访问该控件的所有属性,例如
HasFile
。Instead of instantiating the
FileUpload
in your code behind file, just declare it in your markup file (.aspx file):Then you will be able to access all of the properties of the control, such as
HasFile
.从后面的代码添加 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.
我在代码后面的解决方案是:
我不知道为什么,但是当您在没有 System.Web.UI.WebControls 的情况下使用 FileUpload 时,它引用的不是 YourProject.FileUpload System.Web.UI.WebControls.FileUpload。
My solution in code behind was:
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.
我注意到,当智能对某个对象不起作用时,您正在处理的上面的类中通常会出现错误。
另一种选择是您没有将 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:
is not inside a function in your code behind.
老问题,但仍然,如果它可能对某人有帮助,这里是
在代码隐藏中的完整示例,C# 代码用于抓取文件并将其保存在目录源中
: ASP.NET 中的文件上传(Web 表单上传控件示例)
Old Question, but still, if it might help someone, here is complete sample
In your Code-behind, C# code to grab file and save it in Directory
Source: File Upload in ASP.NET (Web-Forms Upload control example)