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

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

 更新時(shí)間:2024年11月04日 15:13:08   作者:假裝我不帥  
遇到安卓手機(jī)解壓縮文件損壞問(wèn)題時(shí),可以考慮兩種解決方案,方案一是使用SharpCompress庫(kù),它是一個(gè)開(kāi)源項(xiàng)目,能夠提供強(qiáng)大的壓縮與解壓功能,支持多種文件格式,方案二是采用aspose.zip庫(kù),這兩種方法都能有效解決文件損壞的問(wèn)題
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)建一個(gè)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測(cè)試</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)請(qǐng)寫入
            //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();
        }
    }
}

安卓手機(jī)解壓縮出現(xiàn)損壞的問(wè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

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

相關(guān)文章

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

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

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

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

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

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

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

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

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

    .Net插件框架Managed Extensibility Framework簡(jiǎn)介

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

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

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

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

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

    VS 2015開(kāi)發(fā)跨平臺(tái)手機(jī)應(yīng)用的配置教程

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

    .NET學(xué)習(xí)筆記之默認(rèn)依賴注入

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

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

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

最新評(píng)論