您的位置:首页 > 其它

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

2008-06-02 09:57 896 查看
一.多文件上传【客户端处理脚本的例子】

<!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>无标题页(From:网络大本营 Http://www.QQview.com)</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.
4000
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;
        }
    }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐