我的画面上有个button,点击之后会调用画面上input file的click()函数,这些都很正常。画面上还有个服务器按钮,点击之后会执行后台的一些操作。
现在的问题是,点击服务器按钮之后他会先清空了input file中的内容,第二次点击之后才执行后台代码。结果后台接收不到input file中的内容。如果不执行click()函数,点击一次服务器按钮就会直接执行后台代码。不知道为什么。请教大家了。部分代码<%@ 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>
<style type ="text/css">
.class1 {filter:alpha(opacity=20); }
</style>
<script type="text/javascript"> var num = 0;
function addFile() {
if(document.getElementById("filename").style.border != "0"){
document.getElementById("filename").style.border = "0";
document.getElementById("button").style.display = "none";
} if (num > 0) {
var _idtext = "text" + (num-1).toString();
var _idbutton = "button" + (num - 1).toString();
document.getElementById(_idtext).style.border = "0";
document.getElementById(_idbutton).style.display = "none";
}
var idfile = "file" + num.toString();
var div = document.createElement("div");
var f = document.createElement("input");
f.setAttribute("type", "file");
f.setAttribute("name", "file");
f.setAttribute("size", "50");
f.setAttribute("id", idfile);
div.appendChild(f); var idtext = "text" + num.toString();
var t = document.createElement("input");
t.setAttribute("type", "text");
t.setAttribute("size", "50");
t.setAttribute("id", idtext);
div.appendChild(t); var idbutton = "button" + num.toString();
var b = document.createElement("input");
b.setAttribute("type", "button");
b.setAttribute("id", idbutton);
b.setAttribute("value", "参照..");
div.appendChild(b);
document.getElementById("_container").appendChild(div); document.getElementById(idfile).style.display = "none";
document.getElementById(idbutton).onclick = function() { openBrowse(idfile, idtext); };
num = num + 1;
} function deteFile(o) {
while (o.tagName != "div") o = o.parentNode;
o.parentNode.removeChild(o);
} function openBrowse(file, filename) {
var ie = navigator.appName == "Microsoft Internet Explorer" ? true : false;
if (ie) {
document.getElementById(file).click();
document.getElementById(filename).value = document.getElementById(file).value;
}
else {
var a = document.createEvent("MouseEvents");
//FF的处理
a.initEvent("click", true, true);
document.getElementById(file).dispatchEvent(a);
}
addFile();
} </script>
</head>
<body>
<form id="form1" runat="server" method="post" enctype="multipart/form-data">
<h3>MultiFile Upload</h3>
User Name:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<div id="_container">
<input type="text" size="50" id="filename"/>
<input type="file" id="file"name="file" />
<input type="button" name="button" id = "button" value="参照.." onclick="openBrowse('file','filename');" />
</div>
<div style="padding:10px 0">
<asp:Button runat="server" Text="Upload" ID="UploadButton"
onclick="UploadButton_Click"></asp:Button>
</div>
<div>
<asp:Label ID="strStatus" runat="server" Font-Names="宋体" Font-Bold="True" Font-Size="9pt"
Width="500px" BorderStyle="None" BorderColor="White"></asp:Label>
</div>
</form></body>
</html>其实想实现的就是多文件上传,隐藏input file控件。
现在的问题是,点击服务器按钮之后他会先清空了input file中的内容,第二次点击之后才执行后台代码。结果后台接收不到input file中的内容。如果不执行click()函数,点击一次服务器按钮就会直接执行后台代码。不知道为什么。请教大家了。部分代码<%@ 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>
<style type ="text/css">
.class1 {filter:alpha(opacity=20); }
</style>
<script type="text/javascript"> var num = 0;
function addFile() {
if(document.getElementById("filename").style.border != "0"){
document.getElementById("filename").style.border = "0";
document.getElementById("button").style.display = "none";
} if (num > 0) {
var _idtext = "text" + (num-1).toString();
var _idbutton = "button" + (num - 1).toString();
document.getElementById(_idtext).style.border = "0";
document.getElementById(_idbutton).style.display = "none";
}
var idfile = "file" + num.toString();
var div = document.createElement("div");
var f = document.createElement("input");
f.setAttribute("type", "file");
f.setAttribute("name", "file");
f.setAttribute("size", "50");
f.setAttribute("id", idfile);
div.appendChild(f); var idtext = "text" + num.toString();
var t = document.createElement("input");
t.setAttribute("type", "text");
t.setAttribute("size", "50");
t.setAttribute("id", idtext);
div.appendChild(t); var idbutton = "button" + num.toString();
var b = document.createElement("input");
b.setAttribute("type", "button");
b.setAttribute("id", idbutton);
b.setAttribute("value", "参照..");
div.appendChild(b);
document.getElementById("_container").appendChild(div); document.getElementById(idfile).style.display = "none";
document.getElementById(idbutton).onclick = function() { openBrowse(idfile, idtext); };
num = num + 1;
} function deteFile(o) {
while (o.tagName != "div") o = o.parentNode;
o.parentNode.removeChild(o);
} function openBrowse(file, filename) {
var ie = navigator.appName == "Microsoft Internet Explorer" ? true : false;
if (ie) {
document.getElementById(file).click();
document.getElementById(filename).value = document.getElementById(file).value;
}
else {
var a = document.createEvent("MouseEvents");
//FF的处理
a.initEvent("click", true, true);
document.getElementById(file).dispatchEvent(a);
}
addFile();
} </script>
</head>
<body>
<form id="form1" runat="server" method="post" enctype="multipart/form-data">
<h3>MultiFile Upload</h3>
User Name:<asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
<div id="_container">
<input type="text" size="50" id="filename"/>
<input type="file" id="file"name="file" />
<input type="button" name="button" id = "button" value="参照.." onclick="openBrowse('file','filename');" />
</div>
<div style="padding:10px 0">
<asp:Button runat="server" Text="Upload" ID="UploadButton"
onclick="UploadButton_Click"></asp:Button>
</div>
<div>
<asp:Label ID="strStatus" runat="server" Font-Names="宋体" Font-Bold="True" Font-Size="9pt"
Width="500px" BorderStyle="None" BorderColor="White"></asp:Label>
</div>
</form></body>
</html>其实想实现的就是多文件上传,隐藏input file控件。
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货