fileupload控件在ajax中无法使用

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
read this
post
 for 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 much tweaking except for that, there would be a
postback only for the File Upload Control.  While the rest of the
stuff happens 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:ScriptManager ID="ScriptManager1" runat="server"></asp:ScriptManager>

<asp:UpdatePanel ID="UpdatePanel1" runat="server">

<ContentTemplate>

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

<br />

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

<br />

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

<br />

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

<br />

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

<br />

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

<br />

</ContentTemplate>

<Triggers>

<asp:PostBackTrigger ControlID="Button2" />

</Triggers>

</asp:UpdatePanel>

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

protected void Button1_Click(object sender, EventArgs e)

{

Label1.Text
= TextBox1.Text;

}

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;

}

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 is specified 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 would write 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" onclick="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>

fileupload控件在ajax中无法使用,布布扣,bubuko.com

时间: 2024-10-26 12:55:03

fileupload控件在ajax中无法使用的相关文章

使用Anthem.NET 1.5中的FileUpload控件实现Ajax方式的文件上传

Anthem.NET刚刚发布了其最新的1.5版本,其中很不错的一个新功能就是对文件上传功能的Ajax实现.本文将简要介绍一下该功能的使用方法. Anthem.NET的下载与安装 Anthem.NET可以在此下载:http://sourceforge.net/project/showfiles.php?group_id=151897&package_id=168043&release_id=493609 下载之后解压缩至硬盘中的某一目录中,编译项目得到Anthem.dll.然后将其拷贝到We

Asp.net中FileUpload控件实现图片上传并带预览显示

单一图片上传——“选择”+“上传”,.NET默认模式: 1.实现原理: 采用FileUpload控件默认的使用方式,先由“选择”按钮选择图片,然后单击“上传”按钮完成上传,并可在“上传”按钮的单击事件中加载已上传图片. 2.关键代码:     页面代码: 1 <asp:FileUpload ID="FileUpload" runat="server" /> 2 <asp:Button ID="BtnUp" runat="

C# 自定义FileUpload控件

摘要:ASP.NET自带的FileUpload控件会随着浏览器的不同,显示的样式也会发生改变,很不美观,为了提高用户体验度,所以我们会去自定义FileUpload控件 实现思路:用两个Button和TextBox控件来替代FileUpload控件,当点击Button时触发FileUpload控件的点击事件,然后通过JS把FileUpload控件的Value赋给TextBox 代码: aspx文件: 1 <html xmlns="http://www.w3.org/1999/xhtml&qu

Fileupload控件导致500错误

问题: 今天遇到一个问题,用Fileupload控件上传Excel文件,用一个button控件调用“FileUpload1.SaveAs”方法,点击按钮后出现服务器500错误.如下图: 解决方法: 在button事件打断点,调试代码,发现根本没进事件,直接出现500错误,应该不是代码bug. 后来发现文件4.28M,是不是超过默认限制了(默认最大好像是4M)?在配置文件的<system.web>节点下加入了<httpRuntime maxRequestLength="10240

FileUpload控件实现单按钮图片自动上传并带预览显示

FileUpload控件实现单按钮图片自动上传并带预览显示 1.实现原理: FileUpload控件默认不支持服务端的ONCHANGE事件,此时用一种变通的方法借用客户端的onchange事件,调用__doPostBack方法来用LinkButton的OnClick事件模拟一个事件触发的过程,可以在LinkButton的OnClick事件中进行图片的上传,和预览加载. 2.关键代码:      页面代码: 1 <asp:FileUpload ID="fuPhoto" onchan

WebForm之FileUpload控件(文件上传)

FileUpload控件要与Button.LinkButton.ImageButton配合使用 FileUpload控件的方法及属性: 1.SaveAs("要上传到服务器的绝对路径")方法:用来上传文件 注:一般使用Server.MapPath()方法进行相对路径与绝对路径之间的转换. 2.FileName属性:要上传文件的文件名,不带路径 3.PostedFile.ContentLength属性:获得上传文件的字节长度,除以1024,得到KB <一>最简单的上传 //点击

FileUpload控件

FileUpload这个控件我在随笔:<在数据库中 存储图片 以及 在界面中显示图片(存储图片路径)- 这种方法相对与存储二进制文件好> 之中,已经做了介绍,那篇随笔只是介绍上传图片,FIleUpload控件实质上是一个文件上传控件.

FormView控件的InsertItemTemplate中3个DropDownList联动及绑定问题

在InsertItemTemplate中DropDownList联动和绑定不能同时实现,需要去掉SelectedValue='<%# Bind("CompanyID") %>即可实现联动,另外SqlDataSource应该放在InsertItemTemplate中. <%@ Page Title="" Language="VB" MasterPageFile="~/Manage/Site.master" Au

【转载】OLE控件在Direct3D中的渲染方法

原文:OLE控件在Direct3D中的渲染方法 Windows上的图形绘制是基于GDI的, 而Direct3D并不是, 所以, 要在3D窗口中显示一些Windows中的控件会有很多问题 那么, 有什么办法让GDI绘制的内容在3D中显示出来?反正都是图像, 总有办法实现的嘛! 前段时间在研究浏览器在游戏中的嵌入, 基本的思路就是在后台打开一个浏览窗口, 然后把它显示的内容拷贝到一张纹理上, 再把纹理在D3D中绘制出来, 至于事件处理就要另做文章了. 所以, 其它的Windows里的GDI绘制的东西