asp:FileUpload 控件问题 你完全可以不用服务器控件啊,用html的fileupload试试 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 公司后台封装有方法要用FileUpload ,这是一定要用的 我又另外建了一个简单的项目,用于排除别的原因页面<form id="form1" runat="server"> <script> function file(str) { var fileUp=document.getElementById(str); fileUp.click(); } </script> <div> <asp:FileUpload ID="FileUpload1" runat="server" /> <input id="butFileup" type="button" value="浏览" onclick="file('FileUpload1')" /> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> </div> </form>后台protected void Button1_Click(object sender, EventArgs e) { int i = 0; if (i > 0) { Response.Redirect(""); } }断点根本不进入,FileUpload 依然自动清空 试了一下,完全可以啊<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="_20120301_Default2" %><!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"> <script> function file(str) { var fileUp=document.getElementById(str); fileUp.click(); } </script> <div> <asp:FileUpload ID="FileUpload1" runat="server" /> <input id="butFileup" type="button" value="浏览" onclick="file('FileUpload1')" /> <asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" /> </div> </form></body></html> protected void Button1_Click(object sender, EventArgs e) { Response.Write(FileUpload1.FileName); } 是点击input 而不是点击 FileUpload 上的浏览,你在后台打个断点看看,能不能进去...我这里根本不提交,动都不动 看看这个:http://www.quirksmode.org/dom/inputfile.html印象当中input file有特殊控制,必须你手动去点击浏览按钮才可以实现,不记得源自何处了,也就是说自定义实现的是无效的 给你推荐一个我们老大写的jquery插件。可以支持将任意元素扩展为上传控件/// <reference path="jquery-1.4.4.min.js" />/** jQuery插件** 任意元素扩展为单选上传按钮** By hongfei*/(function () { var defaults = { url: '', success: null, error: null, loadBegin: null, loadEnd: null, mouseOver: null, mouseOut: null }; function uploadWrapper($wrapper, options) { this.$wrapper = $wrapper; this.url = options.url; this.success = $.delegate(options.success); this.error = $.delegate(options.error); this.loadBegin = $.delegate(options.loadBegin); this.loadEnd = $.delegate(options.loadEnd); this.mouseOver = $.delegate(options.mouseOver); this.mouseOut = $.delegate(options.mouseOut); this.initPos = {}; this._init(); } uploadWrapper.prototype = { constructor: uploadWrapper, _init: function () { this.$wrapper.removeClass('ui_upload_wrap').addClass('ui_upload_wrap'); this.$image = this.$wrapper.find('img'); if (this.$wrapper.find('.ui_uploader_s_file').length == 0) { this.$input = $('<input name="ui_uploader_s_file" id="ui_uploader_s_file" type="file" hidefocus="true"/>'); this.$wrapper.append(this.$input); } return this._bindEvents(); }, _bindEvents: function () { this.$wrapper.unbind() .mouseover(this._fileFollowBegin.delegate(this)) .mousemove(this._fileFollow.delegate(this)) .mouseout(this._fileFollowEnd.delegate(this)); this.$input = this.$wrapper.find('input').unbind().change(this._uploadImage.delegate(this)); if (this.$btnCancel) { this.$btnCancel.click(_cancelUpload); } return this; }, /// 开始input:file跟随鼠标 _fileFollowBegin: function (e) { this.initPos = this.$wrapper.offset(); this._fileFollow(e); this.mouseOver(); }, /// 使input:file跟随鼠标 _fileFollow: function (e) { this.initPos && this.$input.css({ left: e.pageX - this.initPos.left - 40, top: e.pageY - this.initPos.top - 15 }); }, /// 结束input:file跟随鼠标 _fileFollowEnd: function (e) { this.$input.css({ left: 0, top: 0 }); this.mouseOut(); }, _uploadImage: function () { if (this.$input.val().trim() == '') { return; } var data = { input: this.$input, url: this.url, dataType: 'json', success: this._uploadImageSuccess.delegate(this) }; this.loadBegin(); $.ajaxFile(data); }, _uploadImageSuccess: function (result) { this.loadEnd(); if (result.done) { // 成功时,调用成功回调函数 this.success(result); } else { // 失败时,调用失败回调函数 if (this.error(result) === false) { } else { $.warning(result.msg, '提示'); } } this._bindEvents(); }, _cancelUpload: function () { $.ajaxFile.cancel(); this.$input = this.$wrapper.find('input').unbind().change(this._uploadImage.delegate(this)); } }; $.fn.extend({ uploadWrapper: function (params) { var options = $.mergeObject(defaults, params); this.each(function (i, item) { new uploadWrapper($(item), options); }); } });})();使用方法$('.upload').uploadWrapper({ url: '/Handler/Common/samp.ashx', success: function () { alert('done'); }, error: null, loadBegin: function () { alert('loadBegin'); }, loadEnd: function () { alert('loadEnd'); }, mouseOver: null, mouseOut: null });handler内容HttpPostedFile hpf = context.Request.Files[0]; 服务端控件的button 你没有调用js,不是直接submit了吗。 请问这个问题解决了吗?我也遇到了,javascript中调用那个fileupload的click事件,当调用的时候有内容,之后再调用另一个隐藏的fileupload的click事件的时候就没内容了。 如何实现这个功能 关于打开远程文件 新手高分:如何发布asp.net网站? SQL中的问题 --血累程序员--刚刚工作就碰钉子,好郁闷啊,现在我好想被炒!早点踢走我反而好受。!_! 输出数据到Excel文件,1小时才输出2万数据,算不算慢? 如何在HTML中使用ASPX中的方法呢 关于一个TreeView的隐藏节点的问题 ListBox绑定数据问题! OWC好像只支持RecordSet,可公司的网站项目都用的.NET,我想OWC用该怎么办呢 【ASP.NET】checkboxList 数据绑定问题 想把网页整屏导出到word,样式显示的问题
公司后台封装有方法要用FileUpload ,这是一定要用的
页面<form id="form1" runat="server">
<script>
function file(str)
{
var fileUp=document.getElementById(str);
fileUp.click();
}
</script>
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<input id="butFileup" type="button" value="浏览" onclick="file('FileUpload1')" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>后台protected void Button1_Click(object sender, EventArgs e)
{
int i = 0;
if (i > 0)
{
Response.Redirect("");
}
}
断点根本不进入,FileUpload 依然自动清空
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="_20120301_Default2" %><!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">
<script>
function file(str)
{
var fileUp=document.getElementById(str);
fileUp.click();
}
</script>
<div>
<asp:FileUpload ID="FileUpload1" runat="server" />
<input id="butFileup" type="button" value="浏览" onclick="file('FileUpload1')" />
<asp:Button ID="Button1" runat="server" Text="Button" OnClick="Button1_Click" />
</div>
</form>
</body>
</html>
protected void Button1_Click(object sender, EventArgs e)
{ Response.Write(FileUpload1.FileName);
}
* jQuery插件
*
* 任意元素扩展为单选上传按钮
*
* By hongfei
*/(function () { var defaults = {
url: '',
success: null,
error: null,
loadBegin: null,
loadEnd: null,
mouseOver: null,
mouseOut: null
}; function uploadWrapper($wrapper, options) {
this.$wrapper = $wrapper;
this.url = options.url;
this.success = $.delegate(options.success);
this.error = $.delegate(options.error);
this.loadBegin = $.delegate(options.loadBegin);
this.loadEnd = $.delegate(options.loadEnd);
this.mouseOver = $.delegate(options.mouseOver);
this.mouseOut = $.delegate(options.mouseOut);
this.initPos = {};
this._init();
} uploadWrapper.prototype = {
constructor: uploadWrapper,
_init: function () {
this.$wrapper.removeClass('ui_upload_wrap').addClass('ui_upload_wrap');
this.$image = this.$wrapper.find('img');
if (this.$wrapper.find('.ui_uploader_s_file').length == 0) {
this.$input = $('<input name="ui_uploader_s_file" id="ui_uploader_s_file" type="file" hidefocus="true"/>');
this.$wrapper.append(this.$input);
}
return this._bindEvents();
},
_bindEvents: function () {
this.$wrapper.unbind()
.mouseover(this._fileFollowBegin.delegate(this))
.mousemove(this._fileFollow.delegate(this))
.mouseout(this._fileFollowEnd.delegate(this));
this.$input = this.$wrapper.find('input').unbind().change(this._uploadImage.delegate(this));
if (this.$btnCancel) {
this.$btnCancel.click(_cancelUpload);
}
return this;
},
/// 开始input:file跟随鼠标
_fileFollowBegin: function (e) {
this.initPos = this.$wrapper.offset();
this._fileFollow(e); this.mouseOver();
},
/// 使input:file跟随鼠标
_fileFollow: function (e) {
this.initPos && this.$input.css({
left: e.pageX - this.initPos.left - 40,
top: e.pageY - this.initPos.top - 15
});
},
/// 结束input:file跟随鼠标
_fileFollowEnd: function (e) {
this.$input.css({
left: 0,
top: 0
}); this.mouseOut();
},
_uploadImage: function () {
if (this.$input.val().trim() == '') {
return;
}
var data = {
input: this.$input,
url: this.url,
dataType: 'json',
success: this._uploadImageSuccess.delegate(this)
};
this.loadBegin();
$.ajaxFile(data);
},
_uploadImageSuccess: function (result) {
this.loadEnd();
if (result.done) {
// 成功时,调用成功回调函数
this.success(result);
} else {
// 失败时,调用失败回调函数
if (this.error(result) === false) {
} else {
$.warning(result.msg, '提示');
}
}
this._bindEvents();
},
_cancelUpload: function () {
$.ajaxFile.cancel();
this.$input = this.$wrapper.find('input').unbind().change(this._uploadImage.delegate(this));
}
}; $.fn.extend({
uploadWrapper: function (params) {
var options = $.mergeObject(defaults, params);
this.each(function (i, item) {
new uploadWrapper($(item), options);
});
}
});})();使用方法$('.upload').uploadWrapper({
url: '/Handler/Common/samp.ashx',
success: function () { alert('done'); },
error: null,
loadBegin: function () { alert('loadBegin'); },
loadEnd: function () { alert('loadEnd'); },
mouseOver: null,
mouseOut: null
});handler内容HttpPostedFile hpf = context.Request.Files[0];
请问这个问题解决了吗?我也遇到了,javascript中调用那个fileupload的click事件,当调用的时候有内容,之后再调用另一个隐藏的fileupload的click事件的时候就没内容了。