批量上传图片
ASPX:
<style>
UNKNOWN { FONT-SIZE: 12px; CURSOR: default }
.hand { CURSOR: pointer }
</style> 
<asp:Button id="Button1" runat="server" Text="Button"></asp:Button>
<div id=bxAttList>
<div id=bxAtt style=float:left;width:60>
<span id="bxAtt1" style='position:absolute;padding-top:3;cursor:hand;font-size:12px;width=200px'></span>
<span id="bxAtt2" onmouseover=this.scrollLeft=100 style=position:absolute;width:60;overflow:hidden;filter:alpha(opacity=0);-moz-opacity:0></span>
</div>
</div>
<script type="text/javascript">
var j=<% =aa %>;
file_create();
function file_create(){
$("bxAtt2").innerHTML='<input runat=server onchange=file_change(this) hidefocus type=file size=1 class=hand name=file1>';
num_files();
}
function file_change(tx){
var sName,o;
sName=tx.value.replace(/\\/g,"/").replace(/(.*\/)(.*)/,"$2");
var sName_hz=sName.slice(-3);
if(sName_hz=='jpg'||sName_hz=='gif'){
o=document.createElement("nobr");
o.style.cssText="float:left;margin-right:6;padding-top:3;color:darkgreen;font-size:12px";
o.innerHTML=sName+"<font onclick=removechild(this) class=hand style=color:red;font-weight:bold>"+unescape("%d7")+"</font>";
tx.style.display="none";
o.appendChild(tx);
$("bxAttList").insertBefore(o,$("bxAtt"));
file_repos();
file_create();
num_files();
}
else{
alert("文件类型错误!");
}
}
function removechild(tt)//删除
{
$('bxAttList').removeChild(tt.parentNode);
file_repos();
num_files();
}
function file_repos(){
$("bxAttList").appendChild($("bxAtt"));
}
function $(obj){
return typeof(obj)=="object"?obj:document.getElementById(obj);
}
function num_files(){
var num_file=document.getElementsByName("_ctl0");
if(num_file.length-1==j)
$("bxAtt").style.display="none";
else
$("bxAtt").style.display="";
$("bxAtt1").innerHTML="@添加附件<span style='color:#cccccc'>(您还可上传"+(j-(num_file.length-1))+"个文件)</span>";
}
</script>
CS:
protected System.Web.UI.WebControls.Button Button1;
protected int aa=3;
private void Button1_Click(object sender, System.EventArgs e)
{
System.Web.HttpFileCollection files = System.Web.HttpContext.Current.Request.Files;
int ifile;
string save_name="";
string type="";
for (ifile = 0; ifile < files.Count-1; ifile++)
{
Random random = new Random();
int RandKey=random.Next(1,999);
System.Web.HttpPostedFile myFile = files[ifile];
type = myFile.FileName.Substring(myFile.FileName.LastIndexOf(".")+1).ToLower();
save_name=DateTime.Now.ToString("yyyyMMddHHmmss").ToString()+RandKey.ToString()+"."+type;
//Response.Write(save_name);
myFile.SaveAs(Server.MapPath("ttt")+"\\"+save_name);
}
}


UNKNOWN
浙公网安备 33010602011771号