当前位置: 首页 > 编程日记 > 正文

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>

转载于:https://www.cnblogs.com/drinkoJam/p/3767480.html

相关文章:

Cron表达式的详细用法

字段 允许值 允许的特殊字符 秒 0-59 , - * / 分 0-59 , - * / 小时 0-23 , - * / 日期 1-31 , - * ? / L W C 月份 1-12 或者 JAN-DEC , - * / 星期 1-7 或者 SUN-SAT , - * ? / L C # 年&#xff08;可选&#xff09; 留空, 1970-2099 , - * / 例子&#xff1a;0/5 * * * *…

01-CoreData 概述

CoreData 主要的几个类 1 概述 CoreData 是iOS 3 之后出现的一种数据持久化方案&#xff0c;提供了一种ORM的存储关系&#xff0c;CoreData 是直接将对象存储到数据库中去&#xff0c;也可以将数据库中的数据转化为OC对象&#xff0c;过程中不用编写SQL语句。 2 几个类的讲解…

连信的会话加密密钥协商过程

点击上方↑↑↑蓝字[协议分析与还原]关注我们 “ 介绍连信会话加密密钥的协商过程。” 公号内之前已经对协议的框架和协议内的数据格式进行了介绍&#xff0c;有需要可以回顾下&#xff1a; 连信协议整体框架 连信的protobuf数据格式 本文对连信的会话加密密钥协商过程进行介绍…

6、UmbracoNewsSite:添加新闻分类

上一篇做了一个简单的首页&#xff0c;在导航条上添加了几个新闻板块&#xff0c;接下来我们就添加这几个新闻板块的页面。 开始我们还是需要创建Document Types用来展示新闻列表。我们先做好规划&#xff0c;新闻列表只需要把新闻标题和图片展示出来就可以了&#xff0c;。  …

02-CoreData 的增删改查

CoreData 的增删改查 基本的增删改查的操作 1 数据库的创建 - (void)createDB {// 1.1 创建路径NSURL *modelURL [[NSBundle mainBundle] URLForResource:"AT_CoreDataDemo1" withExtension:"momd"];// 1.2 根据模型文件路径创建模型对象NSManagedObje…

Lemon开源OA

http://www.mossle.com/index.html 转载于:https://www.cnblogs.com/gzmg/p/3770882.html

UC浏览器代理流量解密

点击上方↑↑↑蓝字[协议分析与还原]关注我们“ 解密uc浏览器的安全代理流量&#xff0c;看看是不是真安全。”作为一个从上古网络时代过来的人&#xff0c;一定对UC浏览器有深刻印象&#xff0c;它的流量透明代理功能&#xff0c;就是现在的云加速功能&#xff0c;使用UC的服务…

JAVA07 接口与继承

一 动手实验&#xff1a;继承条件下的构造方法调用 1&#xff09;源代码 1 public class TestInherits {2 3 public static void main(String[] args) {4 // TODO Auto-generated method stub5 6 Child c new Child();7 }8 9 } 10 11 class GrandPa…

学习-现代交换原理与通信技术

帮助学习细化。。。 首先&#xff0c;这书真是难读&#xff0c;像个词典嘛似乎《现代交换原理与通信技术》卞佳丽 北邮 呵呵 呵了个呵 不过词典也有词典的好处 第三章&#xff0c;数字程控电话交换与通信网络 1. 控制子系统 与 话路子系统 前者在于控制&#xff0c;后者在于数…

03-NSPredicate谓词

NSPredicate 谓词 在查询数据的过程中&#xff0c;给NSFetchRequest设置一个过滤条件&#xff0c;不需要讲所有的托管对象加载到内存中去。这样的话就会节省内存和加快查找速度。 一 运算符 1 比较运算符 、< 、 、> 、< 、! 比如说age>80 NSPredicate *pre […

小兵别嚣张,签名算法分析

点击上方↑↑↑蓝字[协议分析与还原]关注我们“分析unity3d游戏小兵别嚣张的签名算法。”不久前&#xff0c;写了个unity3d的引子&#xff0c;介绍了使用工具从unity3d游戏中提取出各种资源和算法&#xff1a;unity3d游戏IL2CPP相关算法简单分析不过内容很少&#xff0c;今天介…

NOIP模拟题——B

【题目描述】我们要从n种食物选m个出来&#xff0c;安排一个顺序吃掉它&#xff08;们&#xff09;&#xff0c;每种食物有个美味值ai&#xff0c;然后我们有k个规则&#xff0c;每个规则有 xi, yi 和 ci三个数&#xff0c;如果吃完第xi种食物接下来马上吃第yi种食物&#xff0…

Maven使用常用命令

> mvn clean删除target文件夹> mvn clean test编译测试代码&#xff0c;默认被放到target/test-classes文件夹下面> mvn clean compile编译主代码&#xff0c;并放到target/classes文件夹下> mvn clean package打包代码&#xff0c;我们可以看到target文件下生成了…

04- CoreData轻量级版本的迁移

CoreData版本的迁移 一 轻量级的数据迁移 例如添加新的实体&#xff0c;新的实体属性。 轻量级版本迁移方案非常简单&#xff0c;大多数迁移工作都是由系统完成的&#xff0c;只需要告诉系统迁移方式即可。在持久化存储协调器(PSC)初始化对应的持久化存储(NSPersistentStore…

通da信TCP长连接数据算法分析

点击上方↑↑↑蓝字[协议分析与还原]关注我们“ 分析通da信TCP长连接内部分数据的算法。”作为一款老牌的炒股软件&#xff0c;通da信里面的数据是相当的丰富&#xff0c;免费的也很丰富&#xff0c;准确性也很好&#xff0c;例如&#xff0c;这种股票之间关联的信息。通da信一…

产品经理怎么样用图表传达数据信息(多图)

上文有点仓促了&#xff0c;结尾没有写好。补上&#xff1a; 对劣质成本分析的时候应该注意&#xff1a; 1、在进行劣质成本分析的时候&#xff0c;要注意区分成本和必要的浪费。要注意一个关系&#xff0c;对必要浪费的控制会导致其他成本的上升。例如前文说到四小时送一次货&…

'This NSPersistentStoreCoordinator has no persistent stores 报错

可能是你改变coredata的属性项之后再运行的话在模拟器中会出现这个问题。找上面说的&#xff0c;找到mac下的模拟器中的程序路径&#xff0c;然后删掉其sqlite文件再运行就好了&#xff01;&#xff01;&#xff01;

tcp断开连接,4次握手,为什么wireshark 只能抓到3个包?

用wireshark 抓包&#xff0c;看看tcp 断开连接的过程. 以前书上说tcp断开连接&#xff0c;4次握手&#xff0c;可我为什么wireshark 只能抓到3个包&#xff1f; 百度一下&#xff0c;别人也有类似的疑问。 【求助】书上和网上的资料说&#xff0c;TCP拆除连接需要四次握手。但…

如何在python开发的GUI界面程序中恰当地使用PyExecJS

点击上方↑↑↑蓝字[协议分析与还原]关注我们“解决一闪而过的黑框的小技巧。”在使用python开发过程中&#xff0c;不可避免地&#xff0c;会开发带界面的应用&#xff0c;也会经常使用js来完成一些功能&#xff0c;比如&#xff0c;我使用python开发个了一个小应用&#xff1…

switch和case的理解

下面是复习java基础知识的时候&#xff0c;发现的一些点&#xff0c;总结下&#xff0c;备忘 int a345;switch (a) {case 23:System.out.println("23"); // break;case 345://条件符合&#xff0c;下面的case条件不会在判断&#xff0c;直接执行&#xff1b;System…

05-自己创建mapmodel自定义迁移方式

自动创建Mapping 如果模型的改变很大或者不支持轻量级数据迁移的条件&#xff0c;则我们需要进行自定义迁移。 使用映射模型 适用于更加复杂的数据的迁移 NSMappingModel 类似于数据模型 NSEntityMapping 告知迁移过程如何在目标数据存储中处理源实体的映射。 映射类型决…

Linux内核之内存管理(4)--缺页处理程序

本文主要解说缺页处理程序&#xff0c;凝视足够具体&#xff0c;不再解释。 //以下函数将一页内存页面映射到指定线性地址处&#xff0c;它返回页面的物理地址 //把一物理内存页面映射到线性地址空间指定处或者说把线性地址空间指定地址address处的页面映射到主内存区页面page上…

WebSocket协议分析

点击上方↑↑↑蓝字[协议分析与还原]关注我们“ 解析websocket数据格式。”好久不见&#xff0c;一晃一年又过去了&#xff0c;祝大家新年好运。今天&#xff0c;给大家分析一个常见的协议——WebSocket&#xff0c;这是一个标准协议&#xff0c;虽然没有HTTP历史悠久&#xff…

《postfix邮件服务下mailq、postmap、postqueue 、 postsuper等用法》

1、Mailq 功能说明&#xff1a;显示待寄邮件的清单。 语  法&#xff1a;mailq [-q] 补充说明&#xff1a;mailq可列出待寄邮件的清单&#xff0c;包括邮件ID&#xff0c;邮件大小&#xff0c;邮件保存时间&#xff0c;寄信人&#xff0c;收信人&#xff0c;以及邮件无法寄出…

[deviceone开发]-一个很炫的手势动画示例

一、简介 这是iOS下的效果&#xff0c;android下完全一致。通过do_GestureView组件和do_Animation组件&#xff0c;deviceone能很容易实现复杂的跨平台纯原生动画效果,这个示例就是通过手势控制图片上下动画滑动实现开合效果&#xff0c;还支持声音效果。 二、效果图 三、相关下…

iOS lldb调试

LLDB 初始 LLDB 是一个有着 REPL 的特性和 C ,Python 插件的开源调试器。LLDB 绑定在 Xcode 内部&#xff0c;存在于主窗口底部的控制台中。调试器允许你在程序运行的特定时暂停它&#xff0c;你可以查看变量的值&#xff0c;执行自定的指令&#xff0c;并且按照你所认为合适的…

万物之中,希望至美

一觉醒来&#xff0c;虎年就这么来了。感谢各位朋友过去的一年与我同在。在新的一年到来之际&#xff0c;我首先做个检讨&#xff0c;过去的一年&#xff0c;我们这个公众号&#xff0c;更新不够频繁&#xff0c;属于三天打鱼两天晒网型公众号&#xff0c;没有为大家带来足够丰…

[转]VS2015编译的程序在其他机器上缺少msvcp120.dll

http://www.lai18.com/content/1159618.html 1、 今天分享一个自己在开发过程中遇到的困难。用VS2015开发了一个windows客户端&#xff08;win32项目&#xff09;&#xff0c;在自己的机器上运行很流畅。当你得意的把releas版本进行打包&#xff0c;并进行发布后&#xff0c;问…

Discuz!的cookie机制

最近在做Discuz!的插件&#xff0c;需要用到cookie&#xff0c;一直觉得奇怪的一个问题&#xff0c;Discuz!大量使用了cookie&#xff0c;但是我在编写插件的时候如果不加入session_start函数cookie就无法使用&#xff0c;按理说Discuz!使用了这么多cookie它的核心应该有调用se…

iOS infoplist 权限设置

<dict> <key>NSAllowsArbitraryLoads</key> <true/> </dict> <key>NSBluetoothPeripheralUsageDescription</key> <string>需要使用您的蓝牙</string> <key>NSCalendarsUsageDes…