fileupload控件在ajax中无法使用

fileupload控件在ajax的UpdatePanel中是没有办法运行的,下面的代码:if (this.FileUpload1.HasFile)总是false、于是google:

错误代码:

前台:

<html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server" > <ContentTemplate> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="Button1" runat="server" Text="Button" οnclick="Btn_Click" /> <asp:Label runat="Server" ID = "Lbl" ></asp:Label> </ContentTemplate> </asp:UpdatePanel> </form> </body> </html>

后台代码:

protected void Btn_Click(object sender, EventArgs e) { if (this.FileUpload1.HasFile) { try { FileUpload1.PostedFile.SaveAs(@"C:/test/" + System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName)); } catch (System.Exception ex) { this.Lbl.Text = ex.Message; } } }

google得到的方法:

1。http://geekswithblogs.net/ranganh/archive/2008/04/01/file-upload-in-updatepanel-asp.net-ajax.aspx

There is a better way of doing it with Ajax Control Toolkit version 3.0.30930 which works with .NET 3.5 SP1 and Visual Studio 2008 SP1. Please readthis postfor a step by step instruction

One of the common queries I get across my sessions is that, the File Upload control doesnt work inside an Update panel. All of us would like to implement a Gmail File Upload kind of interface and when you try to implement a similar thing using UpdatePanel (which works like a charm for other activities), it simply doesn't work.

The behaviour is expected. The File Upload Control doesnt work inside an Update Panel due to security reasons and restrictions a browser implies. They dont allow Javascript files to directly access the files in an user's sytem and dont allow to modify or access the details of a file when working with the File Upload Control.

There are a couple of ways to solve this issue, one using Update Panel and Post Back Triggers and the other using Iframes.

1. Use Update Panel, File Upload Control and use a PostBackTrigger Control to force a postback only for the File Upload Control

This approach works well without muchtweaking except for that, there would be a postback only for the File Upload Control. While the rest of the stuffhappens asynchronously, using the UpdatePanel, when the user presses the "Upload" Button, the page will be refreshed. Let us examine how we can accomplish this. Place the following code within the <form> </form> tags in your ASP.NET Page.

<asp:ScriptManagerID="ScriptManager1"runat="server"></asp:ScriptManager>

<asp:UpdatePanelID="UpdatePanel1"runat="server">

<ContentTemplate>

<asp:TextBoxID="TextBox1"runat="server"></asp:TextBox>

<br/>

<asp:ButtonID="Button1"runat="server"Height="20px"onclick="Button1_Click"Text="Submit"Width="128px"/>

<br/>

<asp:LabelID="Label1"runat="server"Text="Label"></asp:Label>

<br/>

<asp:FileUploadID="FileUpload1"runat="server"/>

<br/>

<asp:ButtonID="Button2"runat="server"Height="25px"onclick="Button2_Click"Text="Upload"Width="128px"/>

<br/>

<asp:LabelID="Label2"runat="server"Text="Label"></asp:Label>

<br/>

</ContentTemplate>

<Triggers>

<asp:PostBackTriggerControlID="Button2"/>

</Triggers>

</asp:UpdatePanel>

In the Code behind, add the following lines of code:-

protectedvoidButton1_Click(objectsender,EventArgse)

{

Label1.Text = TextBox1.Text;

}

protectedvoidButton2_Click(objectsender,EventArgse)

{

FileUpload1.PostedFile.SaveAs(@"C:/test/"+System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName));

Label2.Text = FileUpload1.PostedFile.FileName;

}

If you run the above sample, you would notice that upon entering something in the TextBox and clicking "Submit" (Button1) the Label above the File Upload Content, shows the Text you typed, without a page refresh.

However, when you select a file and click on the "Upload" (Button2) Button, you would notice that a postback happens and the file gets posted to the "C:/Test/"folder and also the full path isspecified in the Label 2.

In the above code, I have not taken any steps regarding validation, checking if file exists etc., since it just shows how you could accomplish File Upload within Update panel. In normal cases, you wouldwrite better code to accomplish a file upload feature.

2. Use Iframes and accomplish a truly Gmail Like File Upload Interface.

I thought of writing a post on this, but did a quick research and found that there are a few solutions posted by our MVPs / Community Folks and just thought of providing a link to the same.

http://vinayakshrestha.wordpress.com/2007/03/13/uploading-files-using-aspnet-ajax-extensions/

http://msmvps.com/blogs/luisabreu/archive/2006/12/14/uploading-files-without-a-full-postback.aspx

Note that this post doesnt claim any warranty / support for the above articles, though.

Cheers !!!

按照上面的第一种方法来做果然成立,下面是相关代码:

前台:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager> <asp:UpdatePanel ID="UpdatePanel1" runat="server"> <ContentTemplate> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="Button2" runat="server" Height="25px" οnclick="Button2_Click" Text="Upload" Width="128px" /> <asp:Label ID="Label2" runat="server" Text="Label"></asp:Label> </ContentTemplate> <Triggers> <asp:PostBackTrigger ControlID="Button2" /> </Triggers> </asp:UpdatePanel> </form> </body> </html>

后台代码:

protected void Button2_Click(object sender, EventArgs e) { FileUpload1.PostedFile.SaveAs(@"C:/test/" + System.IO.Path.GetFileName(FileUpload1.PostedFile.FileName)); Label2.Text = FileUpload1.PostedFile.FileName; }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值