zoukankan      html  css  js  c++  java
  • 多个文件上传控件

    使用:把控件拖到页面中,设置SavePath路径即可上传,如果还要扩展功能请继承此控件。。。

    后台代码:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.IO;

    namespace WebApplication1
    {
    public partial class MutiFileUpload : System.Web.UI.UserControl
    {
    public string SavePath
    {
    get
    {
    if (ViewState["SavePath"] == null)
    {
    ViewState["SavePath"] = Environment.CurrentDirectory;
    }
    return ViewState["SavePath"].ToString();
    }
    set
    {
    if (!Object.Equals(ViewState["SavePath"] , value))
    {
    ViewState["SavePath"] = value;
    }
    }
    }

    protected void Page_Load(object sender, EventArgs e)
    {

    if (!IsPostBack)
    {
    if(this.Page.Form!=null)

    this.Page.Form.Method = "post";
    this.Page.Form.Enctype = "multipart/form-data";
    }
    int count = Request.Files.Count;
    for (int i = 0; i < count; i++)
    {
    HttpPostedFile file = Request.Files[i];
    if (file == null || file.ContentLength <= 0) continue;
    file.SaveAs(Path.Combine(SavePath, Path.GetFileName(file.FileName)));
    }
    }
    }
    }

    前台代码:

    <%@ Control Language="C#" AutoEventWireup="true" CodeBehind="MutiFileUpload.ascx.cs" Inherits="WebApplication1.MutiFileUpload" %>
    <script type="text/javascript">
    function AddFileUpload(o) {
    var panel = document.getElementById("fileUploads");
    var div = document.createElement("div");
    div.innerHtml = "<br/>";
    panel.appendChild(div);
    var fileUpload = document.createElement("input");
    fileUpload.setAttribute("type", "file");
    fileUpload.setAttribute("name", Date().toString());
    fileUpload.setAttribute("id", Date().toString());
    fileUpload.onchange = function() { AddFileUpload(fileUpload); };
    if (o.value != null || o.value != "") {
    panel.appendChild(fileUpload);
    var btnAdd = document.createElement("input");
    btnAdd.value = "X";
    btnAdd.setAttribute("type", "button");
    btnAdd.setAttribute("id", Date().toString());
    btnAdd.onclick = function() { RemoveFileUploadByObject(btnAdd, fileUpload); };
    panel.appendChild(btnAdd);
    }
    return fileUpload;
    }
    function RemoveFileUploadByID(buttonID, fileUploadID) {
    var panel = document.getElementById("fileUploads");
    var fileUpload = document.getElementById(fileUploadID);
    var button = document.getElementById(buttonID);
    RemoveFileUploadByObject(button, fileUpload);
    }
    function RemoveFileUploadByObject(button,fileUpload) {
    var panel = document.getElementById("fileUploads");
    panel.removeChild(button);
    panel.removeChild(fileUpload);
    }
    function submitFunc() {
    document.forms.item(0).submit();
    }
    window.onload = function() {
    // var fileInit = this.parent.document.createElement("input");
    // fileInit.setAttribute("type", "file");
    // fileInit.setAttribute("id", "initFile");
    // fileInit.setAttribute("display", "none");
    // this.parent.document.forms.item(0).appendChild(fileInit);
    var container = this.document.getElementById("fileUploads");
    container.onmousemove = function() {
    this.style.cursor = "progress";
    }
    }
    </script>
    <p id="fileUploads">
    <input type="text" id="AddingFiles" value="AddingFiles"
    style=" padding-right:1px; 75px;" readonly="readonly" /><input id="Button1" type="button" value="AddFiles" onclick="AddFileUpload(this);" /></p>
    <p>
    <input id="Button2" type="submit" value="upload" /></p>

  • 相关阅读:
    Python一些常用模块
    八、线程和进程
    七、Selenium与phantomJS----------动态页面模拟点击、网站模拟登录
    一、scrapy的下载安装---Windows(安装软件太让我伤心了)
    六、BeautifulSoup4------自动登录网站(手动版)
    五、XML与xpath--------------爬取美女图片
    四、正则表达式re模块
    三、Requests库的使用
    二、urllib进阶
    一、爬虫的基本体系和urllib的基本使用
  • 原文地址:https://www.cnblogs.com/hirisw/p/2746015.html
Copyright © 2011-2022 走看看