<section data-swift-gridrow
         data-dw-colorscheme=""
         data-dw-itemtype="cmgtignite_section"
         data-dw-row-space-top="5"
         data-dw-row-space-bottom="5"
         id="n161"
         style="--_section-mobile-items: 1;--_section-mobile-gap: var(--bs-spacing-3);">
  <div class="ignite-section position-relative " style="background-color: var(--swift-background-color);">
    <div data-swift-container data-dw-container-width="3">
      <div class="grid grid-1 row-gap-4  " style="background-color: transparent;">
        <div data-section-container class="ignite-section-layout grid grid-1 grid-xl-1 gap-3 grid-1" data-dw-container-width="3">
            <div class="item">
  <div data-dw-colorscheme="">
	  <pre class='dw-error'>Error compiling template "Designs/Swift-v2/eCom/Productlist/ProductExport.cshtml"
Line 28: The name 'Context' does not exist in the current context
<hr/>
<div class='source-code'>
<span class='line-number'>  1</span>     // &lt;auto-generated/&gt;
<span class='line-number'>  2</span>     #pragma warning disable 1591
<span class='line-number'>  3</span>     namespace CompiledRazorTemplates.Dynamic
<span class='line-number'>  4</span>     {
<span class='line-number'>  5</span>         #line hidden
<span class='line-number'>  6</span>         using System;
<span class='line-number'>  7</span>         using System.Collections.Generic;
<span class='line-number'>  8</span>         using System.Linq;
<span class='line-number'>  9</span>         using System.IO;
<span class='line-number'> 10</span>         using System.IO.Compression;
<span class='line-number'> 11</span>         using System.Text;
<span class='line-number'> 12</span>         using System.Text.Json;
<span class='line-number'> 13</span>         using System.Xml.Linq;
<span class='line-number'> 14</span>         using System.Net.Http;
<span class='line-number'> 15</span>         using System.Threading.Tasks;
<span class='line-number'> 16</span>         using Microsoft.AspNetCore.Http;
<span class='line-number'> 17</span>         using Dynamicweb.Core;
<span class='line-number'> 18</span>         internal class RazorEngine_7347c7ad73da4970be5d6d2ce6191eba : Dynamicweb.Rendering.RazorTemplateBase&lt;Dynamicweb.Rendering.RazorTemplateModel&lt;Dynamicweb.Rendering.Template&gt;&gt;
<span class='line-number'> 19</span>         {
<span class='line-number'> 20</span>             #pragma warning disable 1998
<span class='line-number'> 21</span>             public async override global::System.Threading.Tasks.Task ExecuteAsync()
<span class='line-number'> 22</span>             {
<span class='line-number'> 23</span>                 WriteLiteral(&quot;\n&quot;);
<span class='line-number'> 24</span>                 WriteLiteral(&quot;\n&quot;);
<span class='line-number'> 25</span>                   
    var req = Dynamicweb.Context.Current.Request;
   // var response = Context.Response; // FIX: Changed to Context.Response to resolve HttpContext error
   var context = Context;
    var request = context.Request;
    var response = context.Response;
  bool.TryParse(req.QueryString.Get(&quot;GeneratePdf&quot;), out bool generatePdf);
    int.TryParse(req.Form[&quot;PdfPageId&quot;], out int PdfPageId);
    string languageId = req.Form[&quot;PdfLanguageId&quot;];
    string productId = req.QueryString.Get(&quot;ProductID&quot;);
    string variantId = req.QueryString.Get(&quot;VariantID&quot;);
    string siteUrl = req.Form[&quot;siteUrl&quot;];
    string filename = string.IsNullOrEmpty(productId) ? &quot;export&quot; : productId;
    string exportFormat = req.Form[&quot;format&quot;];
    string exportPurposeName = req.Form[&quot;purpose&quot;];
    var exportPurpose = GetExportPurposeByName(exportPurposeName);
    var images = new List&lt;string&gt;();
    var documents = new List&lt;string&gt;();
    foreach (var key in req.Form.AllKeys) {
        if (key.StartsWith(&quot;Image_&quot;)) images.Add(key.Substring(6));
        else if (key.StartsWith(&quot;Document_&quot;)) documents.Add(key.Substring(9));
    }
    bool generateZip = !generatePdf || images.Count &gt; 0 || documents.Count &gt; 0;
    var fields = new List&lt;Field&gt; {
        new Field(&quot;ID&quot;, &quot;ID&quot;, &quot;Ecom:Product.ID&quot;, FieldType.System),
        new Field(&quot;Name&quot;, &quot;Name&quot;, &quot;Ecom:Product:Field.ProductNameB2B.Value&quot;, FieldType.Standard),
        new Field(&quot;Number&quot;, &quot;Number&quot;, &quot;Ecom:Product.Number&quot;, FieldType.Standard),
        new Field(&quot;ShortDescription&quot;, &quot;Short description&quot;, &quot;Ecom:Product:Field.ProductShortDescriptionB2B&quot;, FieldType.Standard),
        new Field(&quot;LongDescription&quot;, &quot;Long description&quot;, &quot;Ecom:Product:Field.ProductLongDescriptionB2B&quot;, FieldType.Standard),
        new Field(&quot;Price&quot;, &quot;Price&quot;, &quot;Ecom:Product.Price.PriceFormatted&quot;, FieldType.Standard),
        new Field(&quot;Created&quot;, &quot;Created&quot;, &quot;Ecom:Product.Created&quot;, FieldType.System),
        new Field(&quot;Updated&quot;, &quot;Updated&quot;, &quot;Ecom:Product.Updated&quot;, FieldType.System),
        new Field(&quot;Publication&quot;, &quot;Publication Date&quot;, &quot;Ecom:Product.Updated&quot;, FieldType.System)
    };
    foreach (LoopItem customField in GetLoop(&quot;CustomFieldList&quot;)) {
        fields.Add(new Field(
            customField.GetString(&quot;Ecom:CustomField.System&quot;),
            customField.GetString(&quot;Ecom:CustomField.Name&quot;),
            &quot;Ecom:Product:Field.&quot; + customField.GetString(&quot;Ecom:CustomField.Template&quot;),
            FieldType.Custom
        ));
    }
    if (generatePdf) {
        response.ContentType = &quot;application/pdf&quot;;
        response.Headers[&quot;Content-Disposition&quot;] = $&quot;attachment; filename={filename}.pdf&quot;;
        var pdfPath = $&quot;Default.aspx?ID={PdfPageId}&amp;ProductID={productId}&amp;RequestLanguageID={languageId}&amp;pdf=true&amp;VariantID={variantId}&quot;;
        var pdfBytes = await GetBytesFromDocument(pdfPath, siteUrl);
        await response.Body.WriteAsync(pdfBytes); // FIX: Added await to the WriteAsync call
    }
    else if (generateZip) {
        var zipEntries = new Dictionary&lt;string, byte[]&gt;();
        await AddImagesToZipAsync(images, exportPurpose, siteUrl, zipEntries);
        await AddDocumentsToZipAsync(documents, siteUrl, zipEntries);
        string productContent = exportFormat switch {
            &quot;csv&quot; =&gt; GenerateCsv(fields, productId),
            &quot;json&quot; =&gt; GenerateJson(fields, productId),
            &quot;xml&quot; =&gt; GenerateXml(fields, productId),
            _ =&gt; &quot;&quot;
        };
        if (!string.IsNullOrEmpty(productContent))
        {
            zipEntries.Add($&quot;{filename}.{exportFormat}&quot;, Encoding.UTF8.GetBytes(productContent));
        }
        await ZipContentAsync(response, zipEntries, filename);
    }
        }
<span class='line-number'> 26</span>             #pragma warning restore 1998
<span class='line-number'> 27</span>                         
    public enum FieldType {
        System,
        Standard,
        Custom,
        CategoryField
    }
    public class Field {
        public string ID { get; set; }
        public FieldType FieldType { get; set; }
        public string Tag { get; set; }
        public string Label { get; set; }
        public Field(string id, string label, string tag, FieldType type) {
            ID = id;
            Label = label;
            Tag = tag;
            FieldType = type;
        }
    }
    public class ExportImagePurpose {
        public string Name { get; set; }
        public int Resolution { get; set; }
        public int Height { get; set; }
        public int Width { get; set; }
        public string Format { get; set; }
        public bool EmbedICCProfile { get; set; }
        public ExportImagePurpose(string name, int resolution, int height, int width, string format, bool embedIccProfile) {
            Name = name;
            Resolution = resolution;
            Height = height;
            Width = width;
            Format = format;
            EmbedICCProfile = embedIccProfile;
        }
    }
    private List&lt;ExportImagePurpose&gt; _exportPurposes = new List&lt;ExportImagePurpose&gt;() {
        new ExportImagePurpose(&quot;Original&quot;, 0, 0, 0, string.Empty, false),
        new ExportImagePurpose(&quot;Print&quot;, 140, 800, 800, &quot;jpeg&quot;, true),
        new ExportImagePurpose(&quot;Office&quot;, 72, 300, 300, &quot;png&quot;, false),
        new ExportImagePurpose(&quot;Web&quot;, 72, 300, 300, &quot;jpeg&quot;, false),
    };
    public ExportImagePurpose GetExportPurposeByName(string name) {
        return _exportPurposes.FirstOrDefault(p =&gt; p.Name == name);
    }
    public async Task&lt;byte[]&gt; GetBytesFromDocument(string path, string siteUrl) {
        if (!path.StartsWith(&quot;/&quot;))
            path = &quot;/&quot; + path;
        var documentUrl = $&quot;{siteUrl}{path}&quot;;
        using var httpClient = new HttpClient();
        return await httpClient.GetByteArrayAsync(documentUrl);
    }
    public async Task&lt;byte[]&gt; GetBytesFromImage(string path, string siteUrl, ExportImagePurpose exportPurpose) {
        if (!path.StartsWith(&quot;/&quot;))
            path = &quot;/&quot; + path;
        var imageUrl = exportPurpose.Width &gt; 0 &amp;&amp; exportPurpose.Height &gt; 0
            ? $&quot;{siteUrl}/Admin/Public/GetImage.ashx?width={exportPurpose.Width}&amp;height={exportPurpose.Height}&amp;crop=5&amp;Resolution={exportPurpose.Resolution}&amp;Format={exportPurpose.Format}&amp;image={path}&amp;icc={exportPurpose.EmbedICCProfile}&quot;
            : $&quot;{siteUrl}{path}&quot;;
        using var httpClient = new HttpClient();
        return await httpClient.GetByteArrayAsync(imageUrl);
    }
    public string GetImageNameFromUrl(string url) {
        int filenameStartIndex = url.LastIndexOf(&quot;/&quot;) + 1;
        return url.Substring(filenameStartIndex);
    }
    public async Task ZipContentAsync(HttpResponse response, Dictionary&lt;string, byte[]&gt; zipEntries, string filename) {
        response.ContentType = &quot;application/zip&quot;;
        response.Headers[&quot;Content-Disposition&quot;] = $&quot;attachment; filename={filename}.zip&quot;;
        using (var memoryStream = new MemoryStream()) {
            using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) {
                foreach (var zipEntry in zipEntries) {
                    var entry = archive.CreateEntry(zipEntry.Key);
                    using (var entryStream = entry.Open()) {
                        await entryStream.WriteAsync(zipEntry.Value, 0, zipEntry.Value.Length);
                    }
                }
            }
            memoryStream.Position = 0;
            await memoryStream.CopyToAsync(response.Body);
        }
    }
    public async Task AddImagesToZipAsync(List&lt;string&gt; images, ExportImagePurpose exportPurpose, string siteUrl, Dictionary&lt;string, byte[]&gt; zipEntries) {
        foreach (var image in images) {
            var imageFormat = exportPurpose.Format;
            var oldFilename = GetImageNameFromUrl(image);
            var imageName = Path.GetFileNameWithoutExtension(oldFilename);
            var imageBytes = await GetBytesFromImage(image, siteUrl, exportPurpose);
            if (exportPurpose.Width == 0 &amp;&amp; exportPurpose.Height == 0) {
                imageFormat = Path.GetExtension(image).TrimStart(&#39;.&#39;);
            }
            var newFileName = $&quot;{imageName}.{imageFormat}&quot;;
            if (!zipEntries.ContainsKey(newFileName)) {
                zipEntries.Add(newFileName, imageBytes);
            }
        }
    }
    public async Task AddDocumentsToZipAsync(List&lt;string&gt; documents, string siteUrl, Dictionary&lt;string, byte[]&gt; zipEntries) {
        foreach (var document in documents) {
            var oldFilename = GetImageNameFromUrl(document);
            var documentBytes = await GetBytesFromDocument(document, siteUrl);
            if (!zipEntries.ContainsKey(oldFilename)) {
                zipEntries.Add(oldFilename, documentBytes);
            }
        }
    }
    public string GenerateCsv(IEnumerable&lt;Field&gt; fields, string productId) {
        var sb = new StringBuilder();
        sb.Append(Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble()));
        var header = string.Join(&quot;;&quot;, fields.Select(f =&gt; $&quot;\&quot;{f.Label}\&quot;&quot;));
        sb.AppendLine(header);
        if (string.IsNullOrEmpty(productId)) {
            foreach (LoopItem product in GetLoop(&quot;Products&quot;))
                this.AppendCsvLine(sb, fields, product); // FIX: Added this.
        } else {
            this.AppendCsvLine(sb, fields, this); // FIX: Added this.
        }
        return sb.ToString();
    }
    private void AppendCsvLine(StringBuilder sb, IEnumerable&lt;Field&gt; fields, dynamic product) {
        var line = string.Join(&quot;;&quot;, fields.Select(f =&gt; $&quot;\&quot;{product.GetString(f.Tag).Replace(&quot;\&quot;&quot;, &quot;&#39;&quot;)}\&quot;&quot;));
        sb.AppendLine(line);
    }
    public string GenerateJson(IEnumerable&lt;Field&gt; fields, string productId) {
        var productFeed = new List&lt;dynamic&gt;();
        if (string.IsNullOrEmpty(productId)) {
            foreach (LoopItem product in GetLoop(&quot;Products&quot;))
                this.AddJsonProduct(productFeed, fields, product); // FIX: Added this.
        } else {
            this.AddJsonProduct(productFeed, fields, this); // FIX: Added this.
        }
        return JsonSerializer.Serialize(productFeed);
    }
    private void AddJsonProduct(List&lt;dynamic&gt; list, IEnumerable&lt;Field&gt; fields, dynamic product) {
        var obj = new Dictionary&lt;string, object&gt;();
        foreach (var f in fields) obj[f.Label] = product.GetString(f.Tag);
        list.Add(obj);
    }
    public string GenerateXml(IEnumerable&lt;Field&gt; fields, string productId) {
        var xdoc = new XDocument();
        var root = new XElement(&quot;Root&quot;);
        if (string.IsNullOrEmpty(productId)) {
            foreach (LoopItem product in GetLoop(&quot;Products&quot;))
                this.AddXmlProduct(root, fields, product); // FIX: Added this.
        } else {
            this.AddXmlProduct(root, fields, this); // FIX: Added this.
        }
        xdoc.Add(root);
        return xdoc.ToString();
    }
    private void AddXmlProduct(XElement root, IEnumerable&lt;Field&gt; fields, dynamic product) {
        var productElement = new XElement(&quot;Product&quot;);
        foreach (var field in fields) {
            productElement.Add(new XElement(field.ID, product.GetString(field.Tag)));
        }
        root.Add(productElement);
    }
    }
<span class='line-number'> 28</span>     }
<span class='line-number'> 29</span>     #pragma warning restore 1591
<span class='line-number'> 30</span>     
</div>
<hr/>
<div class='template-code'>
<span class='line-number'>  1</span>     @inherits Dynamicweb.Rendering.RazorTemplateBase&lt;Dynamicweb.Rendering.RazorTemplateModel&lt;Dynamicweb.Rendering.Template&gt;&gt;
@using System.IO;
@using System.IO.Compression;
@using System.Text;
@using System.Text.Json;
@using System.Xml.Linq;
@using System.Net.Http;
@using System.Threading.Tasks;
@using Microsoft.AspNetCore.Http;
@using Dynamicweb.Core
@functions {
    public enum FieldType {
        System,
        Standard,
        Custom,
        CategoryField
    }
    public class Field {
        public string ID { get; set; }
        public FieldType FieldType { get; set; }
        public string Tag { get; set; }
        public string Label { get; set; }
        public Field(string id, string label, string tag, FieldType type) {
            ID = id;
            Label = label;
            Tag = tag;
            FieldType = type;
        }
    }
    public class ExportImagePurpose {
        public string Name { get; set; }
        public int Resolution { get; set; }
        public int Height { get; set; }
        public int Width { get; set; }
        public string Format { get; set; }
        public bool EmbedICCProfile { get; set; }
        public ExportImagePurpose(string name, int resolution, int height, int width, string format, bool embedIccProfile) {
            Name = name;
            Resolution = resolution;
            Height = height;
            Width = width;
            Format = format;
            EmbedICCProfile = embedIccProfile;
        }
    }
    private List&lt;ExportImagePurpose&gt; _exportPurposes = new List&lt;ExportImagePurpose&gt;() {
        new ExportImagePurpose(&quot;Original&quot;, 0, 0, 0, string.Empty, false),
        new ExportImagePurpose(&quot;Print&quot;, 140, 800, 800, &quot;jpeg&quot;, true),
        new ExportImagePurpose(&quot;Office&quot;, 72, 300, 300, &quot;png&quot;, false),
        new ExportImagePurpose(&quot;Web&quot;, 72, 300, 300, &quot;jpeg&quot;, false),
    };
    public ExportImagePurpose GetExportPurposeByName(string name) {
        return _exportPurposes.FirstOrDefault(p =&gt; p.Name == name);
    }
    public async Task&lt;byte[]&gt; GetBytesFromDocument(string path, string siteUrl) {
        if (!path.StartsWith(&quot;/&quot;))
            path = &quot;/&quot; + path;
        var documentUrl = $&quot;{siteUrl}{path}&quot;;
        using var httpClient = new HttpClient();
        return await httpClient.GetByteArrayAsync(documentUrl);
    }
    public async Task&lt;byte[]&gt; GetBytesFromImage(string path, string siteUrl, ExportImagePurpose exportPurpose) {
        if (!path.StartsWith(&quot;/&quot;))
            path = &quot;/&quot; + path;
        var imageUrl = exportPurpose.Width &gt; 0 &amp;&amp; exportPurpose.Height &gt; 0
            ? $&quot;{siteUrl}/Admin/Public/GetImage.ashx?width={exportPurpose.Width}&amp;height={exportPurpose.Height}&amp;crop=5&amp;Resolution={exportPurpose.Resolution}&amp;Format={exportPurpose.Format}&amp;image={path}&amp;icc={exportPurpose.EmbedICCProfile}&quot;
            : $&quot;{siteUrl}{path}&quot;;
        using var httpClient = new HttpClient();
        return await httpClient.GetByteArrayAsync(imageUrl);
    }
    public string GetImageNameFromUrl(string url) {
        int filenameStartIndex = url.LastIndexOf(&quot;/&quot;) + 1;
        return url.Substring(filenameStartIndex);
    }
    public async Task ZipContentAsync(HttpResponse response, Dictionary&lt;string, byte[]&gt; zipEntries, string filename) {
        response.ContentType = &quot;application/zip&quot;;
        response.Headers[&quot;Content-Disposition&quot;] = $&quot;attachment; filename={filename}.zip&quot;;
        using (var memoryStream = new MemoryStream()) {
            using (var archive = new ZipArchive(memoryStream, ZipArchiveMode.Create, true)) {
                foreach (var zipEntry in zipEntries) {
                    var entry = archive.CreateEntry(zipEntry.Key);
                    using (var entryStream = entry.Open()) {
                        await entryStream.WriteAsync(zipEntry.Value, 0, zipEntry.Value.Length);
                    }
                }
            }
            memoryStream.Position = 0;
            await memoryStream.CopyToAsync(response.Body);
        }
    }
    public async Task AddImagesToZipAsync(List&lt;string&gt; images, ExportImagePurpose exportPurpose, string siteUrl, Dictionary&lt;string, byte[]&gt; zipEntries) {
        foreach (var image in images) {
            var imageFormat = exportPurpose.Format;
            var oldFilename = GetImageNameFromUrl(image);
            var imageName = Path.GetFileNameWithoutExtension(oldFilename);
            var imageBytes = await GetBytesFromImage(image, siteUrl, exportPurpose);
            if (exportPurpose.Width == 0 &amp;&amp; exportPurpose.Height == 0) {
                imageFormat = Path.GetExtension(image).TrimStart(&#39;.&#39;);
            }
            var newFileName = $&quot;{imageName}.{imageFormat}&quot;;
            if (!zipEntries.ContainsKey(newFileName)) {
                zipEntries.Add(newFileName, imageBytes);
            }
        }
    }
    public async Task AddDocumentsToZipAsync(List&lt;string&gt; documents, string siteUrl, Dictionary&lt;string, byte[]&gt; zipEntries) {
        foreach (var document in documents) {
            var oldFilename = GetImageNameFromUrl(document);
            var documentBytes = await GetBytesFromDocument(document, siteUrl);
            if (!zipEntries.ContainsKey(oldFilename)) {
                zipEntries.Add(oldFilename, documentBytes);
            }
        }
    }
    public string GenerateCsv(IEnumerable&lt;Field&gt; fields, string productId) {
        var sb = new StringBuilder();
        sb.Append(Encoding.UTF8.GetString(Encoding.UTF8.GetPreamble()));
        var header = string.Join(&quot;;&quot;, fields.Select(f =&gt; $&quot;\&quot;{f.Label}\&quot;&quot;));
        sb.AppendLine(header);
        if (string.IsNullOrEmpty(productId)) {
            foreach (LoopItem product in GetLoop(&quot;Products&quot;))
                this.AppendCsvLine(sb, fields, product); // FIX: Added this.
        } else {
            this.AppendCsvLine(sb, fields, this); // FIX: Added this.
        }
        return sb.ToString();
    }
    private void AppendCsvLine(StringBuilder sb, IEnumerable&lt;Field&gt; fields, dynamic product) {
        var line = string.Join(&quot;;&quot;, fields.Select(f =&gt; $&quot;\&quot;{product.GetString(f.Tag).Replace(&quot;\&quot;&quot;, &quot;&#39;&quot;)}\&quot;&quot;));
        sb.AppendLine(line);
    }
    public string GenerateJson(IEnumerable&lt;Field&gt; fields, string productId) {
        var productFeed = new List&lt;dynamic&gt;();
        if (string.IsNullOrEmpty(productId)) {
            foreach (LoopItem product in GetLoop(&quot;Products&quot;))
                this.AddJsonProduct(productFeed, fields, product); // FIX: Added this.
        } else {
            this.AddJsonProduct(productFeed, fields, this); // FIX: Added this.
        }
        return JsonSerializer.Serialize(productFeed);
    }
    private void AddJsonProduct(List&lt;dynamic&gt; list, IEnumerable&lt;Field&gt; fields, dynamic product) {
        var obj = new Dictionary&lt;string, object&gt;();
        foreach (var f in fields) obj[f.Label] = product.GetString(f.Tag);
        list.Add(obj);
    }
    public string GenerateXml(IEnumerable&lt;Field&gt; fields, string productId) {
        var xdoc = new XDocument();
        var root = new XElement(&quot;Root&quot;);
        if (string.IsNullOrEmpty(productId)) {
            foreach (LoopItem product in GetLoop(&quot;Products&quot;))
                this.AddXmlProduct(root, fields, product); // FIX: Added this.
        } else {
            this.AddXmlProduct(root, fields, this); // FIX: Added this.
        }
        xdoc.Add(root);
        return xdoc.ToString();
    }
    private void AddXmlProduct(XElement root, IEnumerable&lt;Field&gt; fields, dynamic product) {
        var productElement = new XElement(&quot;Product&quot;);
        foreach (var field in fields) {
            productElement.Add(new XElement(field.ID, product.GetString(field.Tag)));
        }
        root.Add(productElement);
    }
}
@{
    var req = Dynamicweb.Context.Current.Request;
   // var response = Context.Response; // FIX: Changed to Context.Response to resolve HttpContext error
   var context = Context;
    var request = context.Request;
    var response = context.Response;
  bool.TryParse(req.QueryString.Get(&quot;GeneratePdf&quot;), out bool generatePdf);
    int.TryParse(req.Form[&quot;PdfPageId&quot;], out int PdfPageId);
    string languageId = req.Form[&quot;PdfLanguageId&quot;];
    string productId = req.QueryString.Get(&quot;ProductID&quot;);
    string variantId = req.QueryString.Get(&quot;VariantID&quot;);
    string siteUrl = req.Form[&quot;siteUrl&quot;];
    string filename = string.IsNullOrEmpty(productId) ? &quot;export&quot; : productId;
    string exportFormat = req.Form[&quot;format&quot;];
    string exportPurposeName = req.Form[&quot;purpose&quot;];
    var exportPurpose = GetExportPurposeByName(exportPurposeName);
    var images = new List&lt;string&gt;();
    var documents = new List&lt;string&gt;();
    foreach (var key in req.Form.AllKeys) {
        if (key.StartsWith(&quot;Image_&quot;)) images.Add(key.Substring(6));
        else if (key.StartsWith(&quot;Document_&quot;)) documents.Add(key.Substring(9));
    }
    bool generateZip = !generatePdf || images.Count &gt; 0 || documents.Count &gt; 0;
    var fields = new List&lt;Field&gt; {
        new Field(&quot;ID&quot;, &quot;ID&quot;, &quot;Ecom:Product.ID&quot;, FieldType.System),
        new Field(&quot;Name&quot;, &quot;Name&quot;, &quot;Ecom:Product:Field.ProductNameB2B.Value&quot;, FieldType.Standard),
        new Field(&quot;Number&quot;, &quot;Number&quot;, &quot;Ecom:Product.Number&quot;, FieldType.Standard),
        new Field(&quot;ShortDescription&quot;, &quot;Short description&quot;, &quot;Ecom:Product:Field.ProductShortDescriptionB2B&quot;, FieldType.Standard),
        new Field(&quot;LongDescription&quot;, &quot;Long description&quot;, &quot;Ecom:Product:Field.ProductLongDescriptionB2B&quot;, FieldType.Standard),
        new Field(&quot;Price&quot;, &quot;Price&quot;, &quot;Ecom:Product.Price.PriceFormatted&quot;, FieldType.Standard),
        new Field(&quot;Created&quot;, &quot;Created&quot;, &quot;Ecom:Product.Created&quot;, FieldType.System),
        new Field(&quot;Updated&quot;, &quot;Updated&quot;, &quot;Ecom:Product.Updated&quot;, FieldType.System),
        new Field(&quot;Publication&quot;, &quot;Publication Date&quot;, &quot;Ecom:Product.Updated&quot;, FieldType.System)
    };
    foreach (LoopItem customField in GetLoop(&quot;CustomFieldList&quot;)) {
        fields.Add(new Field(
            customField.GetString(&quot;Ecom:CustomField.System&quot;),
            customField.GetString(&quot;Ecom:CustomField.Name&quot;),
            &quot;Ecom:Product:Field.&quot; + customField.GetString(&quot;Ecom:CustomField.Template&quot;),
            FieldType.Custom
        ));
    }
    if (generatePdf) {
        response.ContentType = &quot;application/pdf&quot;;
        response.Headers[&quot;Content-Disposition&quot;] = $&quot;attachment; filename={filename}.pdf&quot;;
        var pdfPath = $&quot;Default.aspx?ID={PdfPageId}&amp;ProductID={productId}&amp;RequestLanguageID={languageId}&amp;pdf=true&amp;VariantID={variantId}&quot;;
        var pdfBytes = await GetBytesFromDocument(pdfPath, siteUrl);
        await response.Body.WriteAsync(pdfBytes); // FIX: Added await to the WriteAsync call
    }
    else if (generateZip) {
        var zipEntries = new Dictionary&lt;string, byte[]&gt;();
        await AddImagesToZipAsync(images, exportPurpose, siteUrl, zipEntries);
        await AddDocumentsToZipAsync(documents, siteUrl, zipEntries);
        string productContent = exportFormat switch {
            &quot;csv&quot; =&gt; GenerateCsv(fields, productId),
            &quot;json&quot; =&gt; GenerateJson(fields, productId),
            &quot;xml&quot; =&gt; GenerateXml(fields, productId),
            _ =&gt; &quot;&quot;
        };
        if (!string.IsNullOrEmpty(productContent))
        {
            zipEntries.Add($&quot;{filename}.{exportFormat}&quot;, Encoding.UTF8.GetBytes(productContent));
        }
        await ZipContentAsync(response, zipEntries, filename);
    }
}
</div>
</pre>
  </div>
            </div>
        </div>
      </div>
    </div>
  </div>
</section>
