孟子e章:请帮忙解决改动<在ASP.NET中实现多文件上传>中出现的问题
原文出处:http://dotnet.aspx.cc/article/58EA3515-36F2-4FD9-AC89-EAF49F59816C/read.aspx
我想改动为:页面中可以选择上传文字或图片到指定图据库中,那么当点击图片上传后,如果重新选择上传文字,上次上传的图片也会再次上传。
我把我的源程试过一次,只要刷新页面,就是重复上传,就问有没有办法改进这个办法。同时我也向你的信箱发了一封信,肯求解决。最好你把加我QQ,帮我解决一下这个问题。谢谢
我把界面试设计部分贴出来吧,希望你能理解我的用意。
<%@ 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>
<script language= "JavaScript ">
function addFile()
{
var str = ' <br> <INPUT type= "file " size= "50 " NAME= "File "> '
document.getElementById( 'Panel1 ').insertAdjacentHTML( "beforeEnd ",str)
}
</script>
</head>
<body style= "text-align:center; ">
<form id= "form1 " runat= "server " enctype= "multipart/form-data ">
<div>
<table border= "0 " width= "750 ">
<tr>
<td align= "left "> 所属类别 </td>
<td width= "250px ">
<asp:DropDownList ID= "ddlCategory " runat= "server " Width= "250px " DataTextField= "CategoryName " DataValueField= "CategoryID " OnSelectedIndexChanged= "ddlCategory_SelectedIndexChanged " AutoPostBack= "true ">
</asp:DropDownList> </td>
<td> 小说名称 </td>
<td style= "width: 250px ">
<asp:DropDownList ID= "ddlBookName " runat= "server " Width= "250px " DataTextField= "BookName " DataValueField= "BookID ">
</asp:DropDownList> </td>
</tr>
<tr>
<td align= "left ">
章节名称 </td>
<td colspan= "3 ">
<asp:TextBox ID= "txtChapterName " runat= "server " Width= "99% "> </asp:TextBox> </td>
</tr>
<tr>
<td style= "height: 18px " align= "left ">
信息属性 </td>
<td colspan= "3 " style= "height: 18px ">
文字 <asp:RadioButton ID= "radText " runat= "server " GroupName= "isPicture " Checked= "True " OnCheckedChanged= "radText_CheckedChanged " AutoPostBack= "true "/>
图片 <asp:RadioButton ID= "radPicture " runat= "server " GroupName= "isPicture " AutoPostBack= "true " OnCheckedChanged= "radPicture_CheckedChanged " /> </td>
</tr>
</table>
<asp:TextBox ID= "txtContent " runat= "server " Width= "740px " Height= "500px " TextMode= "MultiLine "> </asp:TextBox>
<br />
<asp:Panel ID= "panelPicture " runat= "server " Height= "50px " Width= "750px ">
<INPUT type= "file " size= "50 " NAME= "File "> <br />
<input type= "button " value= "增加(Add) " onclick= "addFile() ">
<input onclick= "this.form.reset() " type= "button " value= "重置(ReSet) ">
<asp:Button Runat= "server " Text= "开始上传 " ID= "UploadButton " OnClick= "UploadButton_Click "> </asp:Button>
</asp:Panel>
<br />
<asp:Button ID= "btnOK " runat= "server " Text= "提交 " OnClick= "btnOK_Click " />
<asp:Button ID= "btnCancel " runat= "server " Text= "取消 " OnClick= "btnCancel_Click " /> <br />
</div>
</form>
</body>
</html>
同时也希望其它高手帮解决一下,这个问题归根到底就是多文件上传问题,如果有类似的源程,帮我贴出,谢谢
[解决办法]
很正常。只要刷新页面,就是重复上传.
上传完毕后Response.Redirect(当前页)
[解决办法]
<FORM id= "form1 " runat= "server ">
<DIV id= "div1 ">
<INPUT ID= "File1 " TYPE= "file " NAME= "File1 " runat= "server ">
<INPUT TYPE= "button " VALUE= "添加附件 " onclick= "javascript:AddFile(); ">
<INPUT TYPE= "button " VALUE= "删除附件 " onclick= "javascript:RemoveFile(); ">
<ASP:LISTBOX id= "ListBox1 " Width= "200px " Height= "100px " runat= "server "> </ASP:LISTBOX>
<ASP:BUTTON id= "Button1 " runat= "server " Text= "保存 " Width= "60px "> </ASP:BUTTON>
</DIV>
<ASP:LITERAL ID= "lResult " Runat= "server "> </ASP:LITERAL>
</FORM>
<SCRIPT language= "javascript ">
<!--
function AddFile()
{
var file = document.getElementById( "div1 ").firstChild;
if(file.value == " ")
{
alert( "请选择文件! ");
return;
}
var ary = file.value.split( "\ ");
var filename = ary[ary.length-1];
var bAddFile = true;
if(CheckOptionsExists(filename,document.getElementById( "ListBox1 ")))
{
alert( "文件已经存在列表中! ");
div1.removeChild(file);
bAddFile = false;
}
var f = document.createElement( "input ");
f.type = "file ";
f.name = "file "
div1.insertBefore(f,div1.firstChild);
if(!bAddFile)
{
return
}
var o = new Option();
o.innerText = filename;
o.value = file.uniqueID;
document.getElementById( "ListBox1 ").appendChild(o);
file.style.display = "none ";
}
function RemoveFile()
{
var lst = document.getElementById( "ListBox1 ");
if(lst.selectedIndex == -1)
{
alert( "请选择要删除的附件! ");
return;
}
var id = lst.value;
div1.removeChild(document.all[id]);
lst.removeChild(lst.options[lst.selectedIndex]);
div1.firstChild.style.display = " ";
}
//检查选项是否存在.
function CheckOptionsExists(value,ddl)
{
for(var i=0;i <ddl.options.length;i++)
{
if(ddl.options[i].innerText == value)
{
return true;
}
}
return false;
}
//-->
</SCRIPT>
后台代码就比较简单了。没有过多的处理,只是一个简单的保存。
private void Button1_Click(object sender, System.EventArgs e)
{
for(int i=0;i <Request.Files.Count;i++)
{
if(Request.Files[i].ContentLength > 0)
{
string filename = System.IO.Path.GetFileName(Request.Files[i].FileName);
Request.Files[i].SaveAs(Server.MapPath(filename));
this.ListBox1.Items.Add(new ListItem(filename,filename));
}
this.lResult.Text = "保存成功! ";
}
}