欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

asp.net core實現(xiàn)在線生成多個文件將多個文件打包為zip返回的操作

 更新時間:2024年11月04日 15:13:08   作者:假裝我不帥  
遇到安卓手機解壓縮文件損壞問題時,可以考慮兩種解決方案,方案一是使用SharpCompress庫,它是一個開源項目,能夠提供強大的壓縮與解壓功能,支持多種文件格式,方案二是采用aspose.zip庫,這兩種方法都能有效解決文件損壞的問題
using Aspose.Words;
using Aspose.Words.Saving;
using System.IO.Compression;
namespace ConsoleApp4
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var html = GetHtml();
            using var memoryStream = new MemoryStream();
            using var zipArchive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true);
            var now = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss");
            for (int i = 0; i < 3; i++)
            {
                var docPath = now + "_" + i + ".docx";
                var entry = zipArchive.CreateEntry(docPath, System.IO.Compression.CompressionLevel.Fastest);
                using var entryStream = entry.Open();
                var bytes = Html2Word(html);
                var stream = new MemoryStream(bytes);
                stream.CopyTo(entryStream);
            }
            memoryStream.Position = 0;
            // 創(chuàng)建一個FileStream,并將MemoryStream的內(nèi)容寫入該文件  
            string filePath = now + ".zip";
            using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                memoryStream.CopyTo(fileStream);
            }
            //如果是asp.net core接口返回,代碼如下
            //return File(memoryStream, "application/zip", filePath);
            Console.WriteLine("壓縮完成");
            Console.ReadKey();
        }
        /// <summary>
        /// 獲取html代碼
        /// </summary>
        /// <returns></returns>
        static string GetHtml()
        {
            var htmlData = @"
<!DOCTYPE html>
<html lang=""zh"">
<head>
    <meta charset=""UTF-8"">
    <meta name=""viewport"" content=""width=device-width, initial-scale=1.0"">
    <title>Aspose測試</title>
    <style>
        table {
            border: 1px solid #000;
        }
    </style>
</head>
<body>
    <table>
        <tr>
            <th>姓名</th>
            <th>年齡</th>
        </tr>
        <tr>
            <td>小明</td>
            <td>20</td>
        </tr>
        <tr>
            <td>小紅</td>
            <td>22</td>
        </tr>
        <tr>
            <td>小華</td>
            <td>18</td>
        </tr>
    </table>
</body>
</html>
";
            return htmlData;
        }
        static byte[] Html2Word(string htmlContent)
        {
            //如果有正版授權(quán)請寫入
            //var memoryStream = new MemoryStream(Convert.FromBase64String(""));
            //var license = new Aspose.Words.License();
            //license.SetLicense(memoryStream);
            var doc = new Aspose.Words.Document();
            doc.RemoveAllChildren();
            Aspose.Words.DocumentBuilder builder = new DocumentBuilder(doc);
            builder.InsertHtml(htmlContent);
            //var now = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss");
            //var docPath = now + ".docx";
            //doc.Save(docPath);
            var resultMemoryStream = new MemoryStream();
            doc.Save(resultMemoryStream, SaveOptions.CreateSaveOptions(SaveFormat.Docx));
            return  resultMemoryStream.ToArray();
        }
    }
}

安卓手機解壓縮出現(xiàn)損壞的問題

方案1 使用SharpCompress

using Aspose.Words;
using Aspose.Words.Saving;
using SharpCompress.Archives.Zip;
using System;
using System.IO;
namespace ZipStu02
{
    internal class Program
    {
        static void Main(string[] args)
        {
            var html = GetHtml();
            var now = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss");
            var archive = ZipArchive.Create();
            for (int i = 0; i < 3; i++)
            {
                var docName = now + "_" + i + ".docx";
                var bytes = Html2Word(html);
                var stream = new MemoryStream(bytes);
                archive.AddEntry(docName, stream);
            }
            string filePath = now + ".zip";
            using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
            {
                archive.SaveTo(fileStream);
            }
            Console.WriteLine("生成成功");
            Console.ReadKey();
        }
    }
}

方案2 使用aspose.zip

//var license = new Aspose.Zip.License();
//license.SetLicense("Aspose.Total.lic");
var html = GetHtml();
//Html2Word(html);
var now = DateTime.Now.ToString("yyyy_MM_dd_HH_mm_ss");
var archive = new Archive();
for (int i = 0; i < 3; i++)
{
    var docName = now + "_" + i + ".docx";
    var bytes = Html2Word(html);
    var stream = new MemoryStream(bytes);
    archive.CreateEntry(docName, stream);
}
string filePath = now + ".zip";
using (FileStream fileStream = new FileStream(filePath, FileMode.Create, FileAccess.Write))
{
    archive.Save(fileStream);
}
Console.WriteLine("生成成功");
Console.ReadKey();

參考

https://docs.aspose.com/zip/net/

https://github.com/adamhathcock/sharpcompress/wiki/API-Examples

到此這篇關于asp.net core實現(xiàn)在線生成多個文件將多個文件打包為zip返回的文章就介紹到這了,更多相關asp.net core在線生成多個文件內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

相關文章

  • ASP.NET對HTML頁面元素進行權(quán)限控制(一)

    ASP.NET對HTML頁面元素進行權(quán)限控制(一)

    界面每個元素的權(quán)限也是需要控制的。比如一個查詢用戶的界面里面有查詢用戶按鈕,添加用戶按鈕,刪除用戶按鈕,不同的角色我們得分配不同的權(quán)限
    2013-12-12
  • 在.NET?6中使用日志組件log4net的方法

    在.NET?6中使用日志組件log4net的方法

    本文詳細講解了Asp.Net?Core中使用日志組件log4net的方法,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-11-11
  • ASP.NET Core 2.0 帶初始參數(shù)的中間件問題及解決方法

    ASP.NET Core 2.0 帶初始參數(shù)的中間件問題及解決方法

    這篇文章主要介紹了ASP.NET Core 2.0 帶初始參數(shù)的中間件問題及解決方法,需要的朋友可以參考下
    2017-10-10
  • 在Apache環(huán)境下成功的運行ASP.NET的注意事項

    在Apache環(huán)境下成功的運行ASP.NET的注意事項

    在Apache環(huán)境下成功的運行ASP.NET的注意事項...
    2007-08-08
  • .Net插件框架Managed Extensibility Framework簡介

    .Net插件框架Managed Extensibility Framework簡介

    這篇文章介紹了.Net插件框架Managed Extensibility Framework,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2022-07-07
  • .net簡單使用Log4net的方法(多個日志配置文件)

    .net簡單使用Log4net的方法(多個日志配置文件)

    log4net是.net中常用的一個日志記錄工具,下面這篇文章主要給大家介紹了關于.net簡單使用Log4net的方法(多個日志配置文件),文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面來一起看看吧
    2018-11-11
  • ASP.NET MVC DropDownList數(shù)據(jù)綁定及使用詳解

    ASP.NET MVC DropDownList數(shù)據(jù)綁定及使用詳解

    DropDownList 控件用于創(chuàng)建下拉列表。DropDownList 控件中的每個可選項都是由 ListItem 元素定義的!該控件支持數(shù)據(jù)綁定
    2012-12-12
  • VS 2015開發(fā)跨平臺手機應用的配置教程

    VS 2015開發(fā)跨平臺手機應用的配置教程

    這篇文章主要給大家介紹了關于VS 2015開發(fā)跨平臺手機應用配置的相關資料,文中通過圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。
    2017-12-12
  • .NET學習筆記之默認依賴注入

    .NET學習筆記之默認依賴注入

    這篇文章主要給大家介紹了關于.NET學習筆記之默認依賴注入的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2021-05-05
  • ASP.NET 多次提交的解決辦法2

    ASP.NET 多次提交的解決辦法2

    對“添加”、“提交”、“保存”、“更新”等按鈕需要對數(shù)據(jù)庫進行寫操作的按鈕,一定要在頁面初始化時加載腳本,防止多次重復點擊
    2008-12-12

最新評論