首页 > 编程语言 >asp.net怎么实现web端上传超大文件

asp.net怎么实现web端上传超大文件

时间:2023-04-03 17:11:14浏览次数:58  
标签:web asp string buffer System 文件夹 new using net

ASP.NET上传文件用FileUpLoad就可以,但是对文件夹的操作却不能用FileUpLoad来实现。

下面这个示例便是使用ASP.NET来实现上传文件夹并对文件夹进行压缩以及解压。

ASP.NET页面设计:TextBox和Button按钮。

编辑

TextBox中需要自己受到输入文件夹的路径(包含文件夹),通过Button实现选择文件夹的问题还没有解决,暂时只能手动输入。

两种方法:生成rar和zip。

1.生成rar

using Microsoft.Win32;

using System.Diagnostics;

    {

 

    }

    ///

   /// 压缩文件

   ///

   /// 需要压缩的文件夹或者单个文件

   /// 生成压缩文件的文件名

   /// 生成压缩文件保存路径

   ///

    protected bool RAR(string DFilePath, string DRARName,string DRARPath)

    {

        String therar;

        RegistryKey theReg;

        Object theObj;

        String theInfo;

        ProcessStartInfo theStartInfo;

        Process theProcess;

        try

        {

 

<span ");<="" span="">

            therar = theObj.ToString();

            theReg.Close();

            therar = therar.Substring(1, therar.Length - 7);

 

            theStartInfo = new ProcessStartInfo();

            theStartInfo.FileName = therar;

            theStartInfo.Arguments = theInfo;

            theStartInfo.WindowStyle = ProcessWindowStyle.Hidden;

            theStartInfo.WorkingDirectory = DRARPath ; //RaR文件的存放目录。

            theProcess = new Process();

            theProcess.StartInfo = theStartInfo;

            theProcess.Start();

            theProcess.WaitForExit();

            theProcess.Close();

            return true;

        }

        catch (Exception ex)

        {

            return false;

        }

    }

 

    ///

    /// 解压缩到指定文件夹

    ///

    /// 压缩文件存在的目录

    /// 压缩文件名称

    /// 解压到文件夹

    ///

    protected bool UnRAR(string RARFilePath,string RARFileName,string UnRARFilePath)

    {

        //解压缩

        String therar;

        RegistryKey theReg;

        Object theObj;

        String theInfo;

        ProcessStartInfo theStartInfo;

        Process theProcess;

        try

         {

 

<span ");<="" span="">

            therar = theObj.ToString();

            theReg.Close();

            therar = therar.Substring(1, therar.Length - 7);

 

            theStartInfo = new ProcessStartInfo();

            theStartInfo.FileName = therar;

            theStartInfo.Arguments = theInfo;

            theStartInfo.WindowStyle = ProcessWindowStyle.Hidden;

            theProcess = new Process();

            theProcess.StartInfo = theStartInfo;

            theProcess.Start();

            return true;

        }

        catch (Exception ex)

         {

             return false;

         }

    }

注:这种方法在在电脑注册表中未找到应有的路径,未实现,仅供参考。

2.生成zip

通过调用类库ICSharpCode.SharpZipLib.dll

该类库可以从网上下载。也可以从本链接下载:SharpZipLib_0860_Bin.zip

增加两个类:Zip.cs和UnZip.cs

(1)Zip.cs

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

 

using System.IO;

using System.Collections;

using ICSharpCode.SharpZipLib.Checksums;

using ICSharpCode.SharpZipLib.Zip;

 

 

    /// <summary>

    /// 功能:压缩文件

    /// creator chaodongwang 2009-11-11

    /// </summary>

    public class Zip

    {

        /// <summary>

        /// 压缩单个文件

        /// </summary>

 

 

 

 

        public void ZipFile(string FileToZip, string ZipedFile, int CompressionLevel)

        {

            //如果文件没有找到,则报错

            if (!System.IO.File.Exists(FileToZip))

            {

 

            }

 

            if (ZipedFile == string.Empty)

            {

 

            }

 

 

            {

 

            }

 

            ////如果指定位置目录不存在,创建该目录

 

            //if (!Directory.Exists(zipedDir))

            //    Directory.CreateDirectory(zipedDir);

 

            //被压缩文件名称

            string filename = FileToZip.Substring(FileToZip.LastIndexOf('\\') + 1);

           

            System.IO.FileStream StreamToZip = new System.IO.FileStream(FileToZip, System.IO.FileMode.Open, System.IO.FileAccess.Read);

            System.IO.FileStream ZipFile = System.IO.File.Create(ZipedFile);

            ZipOutputStream ZipStream = new ZipOutputStream(ZipFile);

            ZipEntry ZipEntry = new ZipEntry(filename);

            ZipStream.PutNextEntry(ZipEntry);

            ZipStream.SetLevel(CompressionLevel);

            byte[] buffer = new byte[2048];

            System.Int32 size = StreamToZip.Read(buffer, 0, buffer.Length);

            ZipStream.Write(buffer, 0, size);

            try

            {

                while (size < StreamToZip.Length)

                {

                    int sizeRead = StreamToZip.Read(buffer, 0, buffer.Length);

                    ZipStream.Write(buffer, 0, sizeRead);

                    size += sizeRead;

                }

            }

            catch (System.Exception ex)

            {

                throw ex;

            }

            finally

            {

                ZipStream.Finish();

                ZipStream.Close();

                StreamToZip.Close();

            }

        }

 

        /// <summary>

        /// 压缩文件夹的方法

        /// </summary>

        public void ZipDir(string DirToZip, string ZipedFile, int CompressionLevel)

        {

            //压缩文件为空时默认与压缩文件夹同一级目录

            if (ZipedFile == string.Empty)

            {

 

 

            }

 

 

            {

 

            }

 

            using (ZipOutputStream zipoutputstream = new ZipOutputStream(File.Create(ZipedFile)))

            {

                zipoutputstream.SetLevel(CompressionLevel);

                Crc32 crc = new Crc32();

                Hashtable fileList = getAllFies(DirToZip);

                foreach (DictionaryEntry item in fileList)

                {

                    FileStream fs = File.OpenRead(item.Key.ToString());

                    byte[] buffer = new byte[fs.Length];

                    fs.Read(buffer, 0, buffer.Length);

                    ZipEntry entry = new ZipEntry(item.Key.ToString().Substring(DirToZip.Length + 1));

                    entry.DateTime = (DateTime)item.Value;

                    entry.Size = fs.Length;

                    fs.Close();

                    crc.Reset();

                    crc.Update(buffer);

                    entry.Crc = crc.Value;

                    zipoutputstream.PutNextEntry(entry);

                    zipoutputstream.Write(buffer, 0, buffer.Length);

                }

            }

        }

 

        /// <summary>

        /// 获取所有文件

        /// </summary>

        /// <returns></returns>

        private Hashtable getAllFies(string dir)

        {

            Hashtable FilesList = new Hashtable();

            DirectoryInfo fileDire = new DirectoryInfo(dir);

            if (!fileDire.Exists)

            {

 

            }

 

            this.getAllDirFiles(fileDire, FilesList);

            this.getAllDirsFiles(fileDire.GetDirectories(), FilesList);

            return FilesList;

        }

        /// <summary>

        /// 获取一个文件夹下的所有文件夹里的文件

        /// </summary>

 

 

        private void getAllDirsFiles(DirectoryInfo[] dirs, Hashtable filesList)

        {

            foreach (DirectoryInfo dir in dirs)

            {

 

                {

                    filesList.Add(file.FullName, file.LastWriteTime);

                }

                this.getAllDirsFiles(dir.GetDirectories(), filesList);

            }

        }

        /// <summary>

        /// 获取一个文件夹下的文件

        /// </summary>

 

 

        private void getAllDirFiles(DirectoryInfo dir, Hashtable filesList)

        {

 

            {

                filesList.Add(file.FullName, file.LastWriteTime);

            }

        }

    }

(2)UnZip.cs

using System.Collections.Generic;

using System.Linq;

using System.Web;

 

/// <summary>

/// 解压文件

/// </summary>

 

using System;

using System.Text;

using System.Collections;

using System.IO;

using System.Diagnostics;

using System.Runtime.Serialization.Formatters.Binary;

using System.Data;

 

using ICSharpCode.SharpZipLib.Zip;

using ICSharpCode.SharpZipLib.Zip.Compression;

using ICSharpCode.SharpZipLib.Zip.Compression.Streams;

 

    /// <summary>

    /// 功能:解压文件

    /// creator chaodongwang 2009-11-11

    /// </summary>

    public class UnZipClass

    {

        /// <summary>

        /// 功能:解压zip格式的文件。

        /// </summary>

 

 

 

        /// <returns>解压是否成功</returns>

        public void UnZip(string zipFilePath, string unZipDir)

        {

            if (zipFilePath == string.Empty)

            {

 

            }

            if (!File.Exists(zipFilePath))

            {

 

            }

            //解压文件夹为空时默认与压缩文件同一级目录下,跟压缩文件同名的文件夹

            if (unZipDir == string.Empty)

                unZipDir = zipFilePath.Replace(Path.GetFileName(zipFilePath), Path.GetFileNameWithoutExtension(zipFilePath));

 

 

            if (!Directory.Exists(unZipDir))

                Directory.CreateDirectory(unZipDir);

 

            using (ZipInputStream s = new ZipInputStream(File.OpenRead(zipFilePath)))

            {

 

                ZipEntry theEntry;

                while ((theEntry = s.GetNextEntry()) != null)

                {

                    string directoryName = Path.GetDirectoryName(theEntry.Name);

                    string fileName = Path.GetFileName(theEntry.Name);

                    if (directoryName.Length > 0)

                    {

                        Directory.CreateDirectory(unZipDir + directoryName);

                    }

 

 

                    if (fileName != String.Empty)

                    {

                        using (FileStream streamWriter = File.Create(unZipDir + theEntry.Name))

                        {

 

                            int size = 2048;

                            byte[] data = new byte[2048];

                            while (true)

                            {

                                size = s.Read(data, 0, data.Length);

                                if (size > 0)

                                {

                                    streamWriter.Write(data, 0, size);

                                }

                                else

                                {

                                    break;

                                }

                            }

                        }

                    }

                }

            }

        }

    }

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Drawing;

 

using Microsoft.Win32;

using System.Diagnostics;

 

 

    public partial class UpLoadForm : System.Web.UI.Page

    {

        protected void Page_Load(object sender, EventArgs e)

        {

 

        }

 

        protected void Button1_Click(object sender, EventArgs e)

        {

<span ")="" 如果输入为空,则弹出提示<="" span="">

            {

");

            }

            else

            {

                //压缩文件夹

                string zipPath = TextBox1.Text.Trim(); //获取将要压缩的路径(包括文件夹)

 

                Zip Zc = new Zip();

                Zc.ZipDir(zipPath, zipedPath, 6);

");

 

                //解压文件夹

                UnZipClass unZip = new UnZipClass();

 

");

 

            }

        }

    }

 效果展示:

 

示例下载地址

源代码文档

asp.net源码下载jsp-springboot源码下载jsp-eclipse源码下载jsp-myeclipse源码下载php源码下载csharp-winform源码下载vue-cli源码下载c++源码下载

详细配置信息及思路

 

标签:web,asp,string,buffer,System,文件夹,new,using,net
From: https://www.cnblogs.com/songsu/p/17283674.html

相关文章

  • .Net Core3.1 API访问进行频次限制
    首先,安装AspNetCore.RateLimitNuGet包。您可以通过NuGet包管理器控制台或VisualStudio的NuGet包管理器来执行此操作。安装后,您将在项目中看到一个名为AspNetCoreRateLimit的文件夹,其中包含中间件的配置类。接下来,您需要在Startup.cs文件中注册中间件。您可以在Configure......
  • 深度学习—AlexNet_CIFAR100代码
        1#导入所需的包2importtorch3#importwandb4importtorch.nnasnn5fromtorchvisionimporttransforms6fromtorch.utils.dataimportDataLoader7fromtorchvision.datasetsimportCIFAR10089#使用Compose容器组合定义图......
  • 1、kubernetes概念
    一、kubernetes简介kubernetes的本质是一组服务器集群。它可以在每个节点上运行特定的程序,来对节点中的容器进行管理。目的是为了实现资源管理的自动化。主要功能如下:1、自我修复:一旦某个容器崩溃,会秒级启动一个新容器2、弹性伸缩:可以根据业务需要,自动对集群中的容器数量进行......
  • 汉源高科INDGS2280-WEB千兆2光8电工业级环网交换机Web Smart Switch
     INDGS2280-WEB是汉源高科(北京)科技有限公司自主研发的一款专门针对工业控制领域的工业级环网交换机产品,该产品支持国际标准IEEE802.3D/W/S标准的STP/RSTP/MSTP协议,以及丰富的网管功能,可以提供的稳定可靠的以太网传输,具有高质量的设计和可靠性。可为用户提供并实现高效带宽、可靠......
  • kubernetes rabbimq3.11.11集群之mqtt插件
    1.概述本文是总结给予k8s的rabbitmq3.11.11集群的mqtt搭建rabbitmq集群搭建详见前一篇文章:kubernetes集群部署rabbimq3.11.112.自动创建mqtt账号密码wrapper-entrypoint.sh#!/usr/bin/envbash(sleep20;\admin_user=bbbbbb;\admin_passwd=111111;\rabbitmq......
  • asp.net mvc5 与图片
    【第一种方式】 HttpPostedFileBasefile1、前端POST方式提交表单@using(Html.BeginForm("Upload","Test",FormMethod.Post,new{enctype="multipart/form-data"})){<inputtype="file"name=......
  • 1 Flask 和pythonweb框架介绍、2 flask快速使用 、3 登录,显示用户信息小案例、4 配置
    目录1Flask和pythonweb框架介绍1.1flask介绍2flask快速使用3登录,显示用户信息小案例3.1login.html3.2home.html3.3detail.html3.4py文件4配置文件方式5路由系统5.1路由本质5.2路由参数add_url_rule5.3转换器1Flask和pythonweb框架介绍#pythonweb框架,本质都一......
  • Flask 和pythonweb框架介绍、flask快速使用、登录,显示用户信息小案例、配置文件方式、
    目录1Flask和pythonweb框架介绍1.1flask介绍2flask快速使用3登录,显示用户信息小案例3.1login.html3.2home.html3.3detail.html3.4py文件4配置文件方式5路由系统5.1路由本质5.2路由参数add_url_rule5.3转换器1Flask和pythonweb框架介绍#pythonweb框架,本质都一......
  • LiveQing流媒体RTMP推流服务-如何获直播流地址 HLS/HTTP-FLV/WS-FLV/WebRTC/RTMP视频
    @目录1、服务说明2、如何一步一步搭建视频直播3、配置鉴权直播间3.1直播间创建3.2获取RTMP推流地址3.3播放流地址4、视频集成方式5、分享页面集成6、视频流地址集成6.1、直播流地址(页面查看获取)6.2、直播流地址(接口调用获取)6.2.1、接口服务地址说明6.2.2、获取直播列表7、RTMP......
  • m基于AlexNet神经网络和GEI步态能量图的步态识别算法MATLAB仿真
    1.算法描述        AlexNet是2012年ImageNet竞赛冠军获得者Hinton和他的学生AlexKrizhevsky设计的。也是在那年之后,更多的更深的神经网络被提出,比如优秀的vgg,GoogLeNet。这对于传统的机器学习分类算法而言,已经相当的出色。Alexnet网络模型于2012年提出。它具有更高维......