文件上传[多文件上传与简单操作文件上传实例(文件上传,自动重命名,自动添加文件夹)]【深海原创】

一.多文件上传【客户端处理脚本的例子】

<! DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
< HTML >
 
< HEAD >
< meta  http-equiv ="Content-Language"  content ="zh-cn" >  
< script >  
 
function mCreateFile(obj)
  
var eF 
  
var mName 
  mFileName.innerHTML
="" 
    
if (obj.id=="File"
    
for (i=0;i<mFile.children.length-1;i++
    

      
if (mFile.children[i].value==""
              mFile.removeChild(mFile.children[i]) 
      }
 
      
else 
      

        mName
=mFile.children[i].value.split("") 
        mFileName.innerHTML+=
"<div id=NameDetail title='"+mName[mName.length-1]+"'>"+mName[mName.length-1]+"</div>" 
      } 
    } 
    mstatus.innerHTML=
"总共有 <b>"+(mFile.children.length-1)+"</b> 个文件等待上传" 
  } 
  
  if (obj.id==
"File_New") { 
  eF=document.createElement('<input type=
"file" name="File" size="23" id=File_New onpropertychange="mCreateFile(this)">') 
  mFile.appendChild(eF) 
  obj.id=
"File" 
  } 
 } 
</script> 
<style> 
table{ 
    FILTER: progid:DXImageTransform.Microsoft.Shadow(direction=135,color=#999999,strength=3); 

input{ 
border:1px soild #000000; 
font-family:Verdana,Arial,宋体; 
font-size:12px; 
padding:2px; 

#mTD{ 
  LINE-HEIGHT: 24px; 

#mFile{ 
width:203px; 
float:left; 

#mFileName{ 
float:right; 
width:182px; 

#NameDetail{ 
overflow:hidden; 
width:176px; 
color:#000000; 
font-family:Verdana,Arial,宋体; 
font-size:12px; 
cursor:default; 
height:22px; 

#mstatus{ 
font-size:12px; 
color:#ff0000; 

</style> 
</head> 
<body> 
<form name=frm method=post> 
<div align=
"center"
<table border=
"1" width="420" style="border-collapse: collapse" bordercolor="#0066cc" cellpadding="4" bgcolor="#D0F0FF"
    <tr> 
        <td bgcolor=
"#3399FF" height="26" align="center"><font color="#FFFFFF" size="3"><b>添加附件</b></font></td> 
    </tr> 
    <tr> 
      <td id=mTD> 
        <div id=mFile><input type=
"file" name="File" size="23" id=File_New onpropertychange="mCreateFile(this)"></div> 
        <div id=mFileName></div> 
        <div id=mstatus>总共有 <b>0</b> 个文件等待上传</div> 
      </td> 
    </tr> 
    </table> 
</div> 
</form> 
</body> 
</html>
后台代码:CS

二.简单操作文件上传实例(文件上传,自动重命名,自动添加文件夹)

前台代码:ASPX

 

<% @ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="Album_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" >
    
< div >
            
< asp:FileUpload  ID ="FileUpload1"  runat ="server"   />
            
< asp:Button  ID ="Button1"  runat ="server"  OnClick ="Button1_Click"  Text ="Button"   />< br  />
            
< asp:Image  ID ="Image1"  runat ="server"  Height ="100px"   />< br  />
            
< asp:Label  ID ="Label1"  runat ="server" ></ asp:Label ></ div >
    
</ form >
</ body >
</ html >

 

 

using  System;
using  System.Data;
using  System.Configuration;
using  System.Collections;
using  System.Web;
using  System.Web.Security;
using  System.Web.UI;
using  System.Web.UI.WebControls;
using  System.Web.UI.WebControls.WebParts;
using  System.Web.UI.HtmlControls;

public   partial   class  Album_Default : System.Web.UI.Page
{
    
protected void Page_Load(object sender, EventArgs e)
    
{
        Image1.Visible 
= false;
    }

    
protected void Button1_Click(object sender, EventArgs e)
    
{
        
string filename = FileUpload1.FileName;
        
string size = FileUpload1.PostedFile.ContentLength.ToString();
        
string[] myfile = filename.Split('.');
        
string dotname = myfile[myfile.Length - 1].ToString().ToLower();
        
string type = FileUpload1.PostedFile.ContentType;
        
string type2 = filename.Substring(filename.LastIndexOf("."+ 1);
        
string imgpath = Server.MapPath("~/Upimg"+ "/";
        
string filepath = Server.MapPath("~/Upfile"+ "/";
        
string folder = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString();
        

        
if (type2 == "jpg" || type2 == "gif")
        
{
            Image1.Visible 
= true;
            
if (!System.IO.Directory.Exists(imgpath + folder))
            
{//自动生成文件夹
                System.IO.Directory.CreateDirectory(imgpath + folder);
            }


            Random myrdn 
= new Random();//产生随机数
            
//日期,时间,随机数和后缀名
            string newfilename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + myrdn.Next(10000).ToString() + "." + dotname;
            FileUpload1.SaveAs(imgpath 
+ folder + "/" + newfilename);
            
string wpath = "~/Upimg/" + folder + "/" + newfilename;
            Image1.ImageUrl 
= wpath;
            
//FileUpload1.SaveAs(ipath);
            Label1.Text = "原始文件名" + filename + "<br>存储文件名:" + newfilename + "<br>文件大小" + size + "<br>文件类型" + type2 + "<br>文件后缀" + type + "<br>文件虚拟路径" + wpath;
        }

        
else
        
{
            
if (!System.IO.Directory.Exists(filepath + folder))
            
{//自动生成文件夹
                System.IO.Directory.CreateDirectory(filepath + folder);
            }


            Random myrdn 
= new Random();//产生随机数
            
//日期,时间,随机数和后缀名
            string newfilename = DateTime.Now.Year.ToString() + DateTime.Now.Month.ToString() + DateTime.Now.Day.ToString() + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + myrdn.Next(10000).ToString() + "." + dotname;
            
            Image1.Visible 
= false;
            
string wpath = "Upfile/" + folder + newfilename;
            FileUpload1.SaveAs(filepath 
+ folder + "/" + newfilename);
            Label1.Text 
= "原始文件名" + filename + "<br>存储文件名:" + newfilename + "<br>文件大小" + size + "<br>文件类型" + type2 + "<br>文件后缀" + type + "<br>文件虚拟路径" + wpath;
        }

    }

}
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值