天天看點

批量下載下傳檔案源代碼

ASP.NET批量下載下傳檔案的方法

這篇文章主要介紹了ASP.NET批量下載下傳檔案的方法,執行個體彙總了常見的asp.net實作批量下載下傳的方法,具有一定的實用價值,需要的朋友可以參考下

本文執行個體講述了ASP.NET批量下載下傳檔案的方法。分享給大家供大家參考。具體方法如下:

一、實作步驟

在使用者操作界面,由使用者選擇需要下載下傳的檔案,系統根據所選檔案,在伺服器上建立用于存儲所選檔案的臨時檔案夾,将所選檔案拷貝至臨時檔案夾。然後調用 RAR程式,對臨時檔案夾進行壓縮,然後輸出到用戶端。最後删除臨時檔案夾。

二、代碼實作

1、ASP.NET批量下載下傳 核心代碼

//周遊伺服器指定檔案夾下的所有檔案

string path = "uploads/Image/";

string serverPath = Server.MapPath(path);

//建立臨時檔案夾

string tempName = DateTime.Now.ToString("yyyyMMddHHMMss");

string tempFolder = Path.Combine(serverPath, tempName);

Directory.CreateDirectory(tempFolder);

DirectoryInfo folder = new DirectoryInfo(serverPath);

foreach(FileInfo file in folder.GetFiles()) {

    string filename = file.Name;

    File.Copy(serverPath + "/" + filename, tempFolder + "/" + filename);

}

//ZKHelper.JSHelper.Alert("圖檔拷貝成功!");

//産生RAR檔案,及檔案輸出

RARSave(tempFolder, tempName);

DownloadRAR(tempFolder + "\\\\" + tempName + ".rar");

2、RARSave(string tempFolder, string tempName) 方法

/// <summary>

/// 生成RAR檔案

/// </summary>

/// <param name="path">存放複制檔案的目錄</param>

/// <param name="rarPatch">RAR檔案存放目錄</param>

/// <param name="rarName">RAR檔案名</param>

private void RARSave(string rarPatch, string rarName) {

    string the_rar;

    RegistryKey the_Reg;

    Object the_Obj;

    string the_Info;

    ProcessStartInfo the_StartInfo;

    Process the_Process;

    try {

        the_Reg = Registry.ClassesRoot.OpenSubKey(@"WinRAR");

        the_Obj = the_Reg.GetValue("");

        the_rar = the_Obj.ToString();

        the_Reg.Close();

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

        the_Info = " a " + rarName + " -r";

        the_StartInfo = new ProcessStartInfo();

        the_StartInfo.FileName = "WinRar"; //the_rar;

        the_StartInfo.Arguments = the_Info;

        the_StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

        //打封包件存放目錄

        the_StartInfo.WorkingDirectory = rarPatch;

        the_Process = new Process();

        the_Process.StartInfo = the_StartInfo;

        the_Process.Start();

        the_Process.WaitForExit();

        the_Process.Close();

    } catch(Exception) {

        throw;

    }

3、DownloadRAR(string file)方法

/// 下載下傳生成的RAR檔案

private void DownloadRAR(string file) {

    FileInfo fileInfo = new FileInfo(file);

    Response.Clear();

    Response.ClearContent();

    Response.ClearHeaders();

    Response.AddHeader("Content-Disposition", "attachment;filename=" + fileInfo.Name);

    Response.AddHeader("Content-Length", fileInfo.Length.ToString());

    Response.AddHeader("Content-Transfer-Encoding", "binary");

    Response.ContentType = "application/octet-stream";

    Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312");

    Response.WriteFile(fileInfo.FullName);

    Response.Flush();

    string tempPath = file.Substring(0, file.LastIndexOf("\\\\"));

    //删除臨時目錄下的所有檔案

    DeleteFiles(tempPath);

    //删除空目錄

    Directory.Delete(tempPath);

    Response.End();

4、DeleteFiles(string tempPath) 方法

/// 删除臨時目錄下的所有檔案

/// <param name="tempPath">臨時目錄路徑</param>

private void DeleteFiles(string tempPath) {

    DirectoryInfo directory = new DirectoryInfo(tempPath);

        foreach(FileInfo file in directory.GetFiles()) {

            if (file.Attributes.ToString().IndexOf("ReadOnly") != -1) {

                file.Attributes = FileAttributes.Normal;

            }

            File.Delete(file.FullName);

        }

JavaScript:

引入頭

<head>

    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>donw2-多檔案示範頁面</title>

    <link type="text/css" href="js/down.css" rel="Stylesheet" />

    <script type="text/javascript" src="js/jquery-1.4.min.js"></script>

    <script type="text/javascript" src="js/down.app.js" charset="utf-8"></script>

    <script type="text/javascript" src="js/down.edge.js" charset="utf-8"></script>

    <script type="text/javascript" src="js/down.file.js" charset="utf-8"></script>

    <script type="text/javascript" src="js/down.folder.js" charset="utf-8"></script>

    <script type="text/javascript" src="js/down.js" charset="utf-8"></script>

</head>

建立down2對象

var downer = new DownloaderMgr();

downer.Config["Folder"] = "";//設定預設下載下傳路徑。

//挂載事件

downer.event.taskCreate = function (obj) { $(document.body).append("檔案ID:" + obj.fileSvr.id) + "<br/>"; };

downer.event.downProcess = function (obj) { };

downer.event.downStoped = function (obj) { };

downer.event.downComplete = function (obj) {

    $(document.body).append('<div>本地路徑:' + obj.fileSvr.pathLoc + '</div>');

};

downer.event.downError = function (obj, err) { };

downer.event.queueComplete = function () { $(document.body).append("<div>隊列完成</div>"); };

批量下載下傳url

$("#btn-down-files").click(function () {

    if (downer.Config["Folder"] == "") { downer.open_folder(); return; }

    var urls = [

        { fileUrl: "http://res2.ncmem.com/res/images/ie11.png" }

        , { fileUrl: "http://res2.ncmem.com/res/images/up6.1/down.png" }

        , { fileUrl: "http://res2.ncmem.com/res/images/firefox.png" }

        , { fileUrl: "http://res2.ncmem.com/res/images/edge.png" }

        , { fileUrl: "http://res2.ncmem.com/res/images/up6.1/cloud.png" }

        , { fileUrl: "http://res2.ncmem.com/res/images/home/w.png" }

        , { fileUrl: "http://res2.ncmem.com/res/images/img.png" }

    ];

    downer.app.addUrls(urls);

});

當成一個檔案夾下載下傳

$("#btn-down-json").click(function () {

    var fd = {

        nameLoc: "圖檔清單"

        , files: [

            { fileUrl: "http://res2.ncmem.com/res/images/ie11.png" }

            , { fileUrl: "http://res2.ncmem.com/res/images/up6.1/down.png" }

            , { fileUrl: "http://res2.ncmem.com/res/images/firefox.png" }

            , { fileUrl: "http://res2.ncmem.com/res/images/edge.png" }

            , { fileUrl: "http://res2.ncmem.com/res/images/up6.1/cloud.png" }

            , { fileUrl: "http://res2.ncmem.com/res/images/home/w.png" }

            , { fileUrl: "http://res2.ncmem.com/res/images/img.png" }

        ]

    };

    downer.app.addJson(fd);

下載下傳多級目錄

$("#btn-down-fd").click(function () {

        nameLoc: "測試檔案夾"

            { fileUrl: "http://www.ncmem.com/images/ico-ftp.jpg" }

            , { fileUrl: "http://www.ncmem.com/images/ico-up.jpg" }

        , folders: [

            {

                nameLoc: "圖檔1"

                , files: [

                    { fileUrl: "http://www.ncmem.com/images/ico-ftp.jpg" }

                    , { fileUrl: "http://www.ncmem.com/images/ico-up.jpg" }

                    , { fileUrl: "http://www.ncmem.com/images/ico-capture.jpg" }

                    , { fileUrl: "http://www.ncmem.com/images/ico-imageuploader.gif" }

                    , { fileUrl: "http://www.ncmem.com/images/ico-wordpaster.gif" }

                ]

                , folders: [

                    {

                        nameLoc: "軟體"

                        , files: [

                            { fileUrl: "http://res2.ncmem.com/res/images/edit-file.png" }

                        ]

                    }

自定義下載下傳檔案名稱

$("#btn-down-svr").click(function () {

        { fileUrl: "http://localhost:90/db/down.aspx", nameLoc: "test.exe" }

        , { fileUrl: "http://localhost:90/db/down.aspx", nameLoc: "test-1.exe" }

最終實作效果:

批量下載下傳檔案源代碼

希望本文所述對大家的asp.net#程式設計有所幫助。