网站导航网学 原创论文 原创专题 网站设计 最新系统 原创论文 论文降重 发表论文 论文发表 UI设计定制 论文答辩PPT格式排版 期刊发表 论文专题
返回网学首页
网学原创论文
最新论文 推荐专题 热门论文 论文专题
当前位置: 网学 > 设计资源 > .Net编程 > 正文

固定格式多文件批量上传

论文降重修改服务、格式排版等 获取论文 论文降重及排版 论文发表 相关服务

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 Index : System.Web.UI.Page
{
    static public ArrayList hif = new ArrayList(); //10001 保存文件列表
    public int filesUploaded = 0; // 10002上传文件的数量
    protected void Page_Load(object sender, EventArgs e)
    {
    }

    //10003-----------------添加列表
    protected void AddFile_Click(object sender, EventArgs e)
    {
        if (Page.IsPostBack==true)  //这个很重要哦.呵呵.按键在第一次PAGE加载后才起作用
        {
            if (FindFile.HasFile)  //判断浏览文本框中有没有文件内容
            {
                hif.Add(FindFile);//把文件完整路径添加到数组中去
                FileList.Items.Add( System.IO.Path.GetFileName(FindFile.PostedFile.FileName)); //获取一个待上传的文件名
            }

            else  
            {
                TipInfo.Text = "<font color=red>错误 - 必须要有上传的文件.</font>";
            
            }

        }

        else
        { }
    }

    //10004从listbox中删除指定的文件
    protected void DelFile_Click(object sender, EventArgs e)
    {
        if (FileList.SelectedIndex == -1)  //if the Listbox''s item is not selected
        {
            TipInfo.Text = "<font color=red>错误 - 必须指定要删除的文件.</font>";
            return;
        }

        else if (FileList.Items.Count != 0)
        {
            hif.RemoveAt(FileList.SelectedIndex); //Remove the item which is selected from the ArryList
            FileList.Items.Remove(FileList.SelectedItem.Text); //Remove the item which is selected in the listbox.
            TipInfo.Text = "";
        }
          
    }

    /// <summary>
    ///10005 循环上传listbox中的文件到指定的文件夹下
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>

    protected void Upload_Click(object sender, EventArgs e)
    {
        string LocalPath = Server.MapPath("UploadFiles/"); // 上传路径  
        string status = "";  // 上传成功后显示的文件列表        
        if ((FileList.Items.Count == 0) && (filesUploaded == 0)) //judge the listbox''item is nothing
        {
            TipInfo.Text = "<font color=red>错误 - 必须指定要上传的图片文件.</font>";
            return;
        }

        else
        {
            foreach (System.Web.UI.WebControls.FileUpload HIF in hif)
                    {
                        try
                        {
                            Boolean fileOK = false;
                            String fileExtension = System.IO.Path.GetExtension(HIF.FileName).ToLower();
                            String allowedExtensions = { ".gif", ".png", ".jpeg", ".jpg" };
                            for (int i = 0; i < allowedExtensions.Length; i++)
                            {
                                if (fileExtension == allowedExtensions)
                                {
                                    fileOK = true;
                                }

                            }

                          
                            if (fileOK)
                            {
                                string fn = System.IO.Path.GetFileName(HIF.PostedFile.FileName);
                                HIF.PostedFile.SaveAs(LocalPath + fn);
                                filesUploaded++;
                                status += fn + "<br>";
                            }

                            else
                            {
                                TipInfo.Text = "<font color=red>对不起.你上传的图片格式不对!</font>";
                                FileList.Items.Clear();
                            }

                        }

                        catch (Exception err)
                        {
                            TipInfo.Text = "上传错误 " + LocalPath
                            + "<br>" + err.ToString();
                        }

                    }

                    if (filesUploaded == hif.Count)
                    {
                        TipInfo.Text = "共上传了 " + filesUploaded + " 个图片文件。 <br>" + status;
                    }

                    hif.Clear();
                    FileList.Items.Clear();
              
                
          
        }

    }

}

示例代码下载

  • 上一篇资讯: CSS盒模式与排版
  • 设为首页 | 加入收藏 | 网学首页 | 原创论文 | 计算机原创
    版权所有 网学网 [Myeducs.cn] 您电脑的分辨率是 像素
    Copyright 2008-2020 myeducs.Cn www.myeducs.Cn All Rights Reserved 湘ICP备09003080号 常年法律顾问:王律师