如何正确使用ASP.NET FileUpload控件 [英] How to correctly use the ASP.NET FileUpload control

查看:171
本文介绍了如何正确使用ASP.NET FileUpload控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图使用ASP.NET FileUpload控件

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;

和我类中,我只是用:

FileUpload fileUpload = new FileUpload();

但是,没有了通常的FileUpload的一部分的属性似乎是可用...如.HasFile。我试图尽在code后面的按钮点击的方法,我已经注意到,大多数.HasFile的用法是在前面的code,但它是我的理解是,这不应该的问题

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.

有谁知道为什么吗?

推荐答案

当你是一个newbe我将与所有的细节去: - )

As you're a newbe I will go with all the details :-)

ASP.NET控件而应放在ASPX标记文件。这是与他们合作的preferred方式。所以加文件上传控件到您的网页。确保它拥有所有必需的属性,包括 ID RUNAT

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

FileUpload1的实例将自动生成/更新*了.Designer.cs文件,该文件是为您的网页的部分类自动创建。通常你不必关心什么是它,只是假设,一个aspx页面上的任何控制自动实例化。

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

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

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.

这篇关于如何正确使用ASP.NET FileUpload控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆