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>

  • 相关阅读:
    Grunt构建工具插件篇——之less工具
    Grunt构建工具能做哪些事?
    Grunt-几个常用的任务配置,加载,执行的写法
    单元测试任务包括哪些?
    单元测试的概念
    分享Grunt.js配置: watch + liveReload 实时监测文件变化自动刷新浏览器
    如何使用Grunt(好文)
    grunt安装详解及失败处理
    利用 Grunt (几乎)无痛地做前端开发 (一)之单元测试
    Grunt实现自动化单元测试
  • 原文地址:https://www.cnblogs.com/hirisw/p/2746015.html
Copyright © 2011-2022 走看看