使用 Acumatica 在按钮操作时生成文本文件并保存到本地驱动器

Generate Text File Upon Button Action and Save to local Drive using Acumatica

我正在尝试在操作按钮上生成一个 文本文件,其中包含在图表中创建的数据视图的内容 Class 并将文件保存在我的本地驱动器中.但是我做不到。

请帮我生成文件...谢谢

我正在使用 Acumatica 版本 2019R2 (v 19.203.0042)

我的代码在这里...

public PXSelect<MayBankGIRO> Document; //this is my dataview
public PXAction<MayBankGiroFilter> createTextFile;
        [PXUIField(DisplayName = "Create Text File")]
        [PXButton()]
        public virtual IEnumerable CreateTextFile(PXAdapter adapter)
        {
            string filepath = "C:\Subhashish Dawn";
            System.IO.StreamWriter sw = new System.IO.StreamWriter(filepath);
            MayBankGIRO giroObject = this.Document.Current;
            List<object> myListObject = new List<object> { };
            FixedLengthFile flatFile = new FixedLengthFile();
            foreach (MayBankGIRO dacRecord in this.Document.Select())
            {
                if (giroObject.ReordType == "00")
                {
                    myListObject.Add(dacRecord.ReordType + "|" + dacRecord.CorporateID + "|" + dacRecord.ClientBatchID + "|");
                }
                else
                {
                    myListObject.Add(dacRecord.ReordType + "|" + dacRecord.CorporateID + "|" + dacRecord.ClientBatchID + "|" + dacRecord.Country + "|");
                    string data = dacRecord.ReordType;

                }

                this.Document.Update(dacRecord);
            }
            flatFile.WriteToFile(myListObject, sw); 

            sw.Flush();
            sw.FlushAsync();

            string path = "DAWN" + ".txt";
            PX.SM.FileInfo file = new PX.SM.FileInfo(Guid.NewGuid(), path, null, System.Text.Encoding.UTF8.GetBytes(**path**)); // what shall i substitite in place of  **path**
            throw new PXRedirectToFileException(file, true);
}
``````````````````````````````````````````````````````
Can anyone please specify what changes in have to make in the above code.


我利用 UploadFileMaintenance 来执行此操作。我不确定这是否能满足您的需求,但这是适合我的核心代码。

byte[] labelBytes = Encoding.ASCII.GetBytes(myLabelData);
if(labelBytes.Length > 0)
{
    string filename = "label-" + Guid.NewGuid().ToString() + ".txt";
    PX.SM.FileInfo labelFileInfo = new FileInfo(filename, null, labelBytes);

    UploadFileMaintenance upload = PXGraph.CreateInstance<UploadFileMaintenance>();

    if (upload.SaveFile(labelFileInfo))
    {
        string targetUrl = PXRedirectToFileException.BuildUrl(labelFileInfo.UID);
        throw new PXRedirectToUrlException(targetUrl, "Print Labels");
    }
}

假设您的 Document 对象是您需要的视图及其 Select() 方法 returns 您文件中需要的所有数据记录,这应该有效:

// We need at least these, listing them for reference
using PX.Data;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

// This is your dataview
public PXSelect<MayBankGIRO> Document; 

// Your action delegate
public PXAction<MayBankGiroFilter> createTextFile;

[PXUIField(DisplayName = "Create Text File")]
[PXButton()]
public virtual IEnumerable CreateTextFile(PXAdapter adapter)
{
    // You can use this method to print debug information for your customizations
    // Just remove when you are done testing
    PXTrace.WriteInformation("Generating records");

    // We will build the content as a string list first
    List<string> myList = new List<string> { };

    // If the value of 'ReordType' can change for each record, you don't need this
    MayBankGIRO giroObject = this.Document.Current;

    foreach (MayBankGIRO dacRecord in this.Document.Select())
    {
        // Does 'ReordType' change for each record?
        // if it does you may need to use 'dacRecord.ReordType' in this if instead
        if (giroObject.ReordType == "00")
        {
            // This only works if all these members are strings or can be cast to strings
            myList.Add(dacRecord.ReordType + "|" + dacRecord.CorporateID + "|" + dacRecord.ClientBatchID + "|");
        }
        else
        {
            // This only works if all these members are strings or can be cast to strings
            myList.Add(dacRecord.ReordType + "|" + dacRecord.CorporateID + "|" + dacRecord.ClientBatchID + "|" + dacRecord.Country + "|");
        }
    }

    PXTrace.WriteInformation("Generating file");

    // Set the name
    string filename = "DAWN" + ".txt";

    // Use our download method
    Download(myList, filename);
}

// We can define a static method to be able to reuse this later for other DACs
public static void Download(List<string> lines, string name)
{
    var bytes = default(byte[]);

    // Write all lines to stream
    using (MemoryStream stream = new MemoryStream())
    {
        StreamWriter sw = new StreamWriter(stream);
        foreach (string line in lines)
        {
            sw.WriteLine(line);
        }
        sw.Close();

        stream.Position = 0;
        bytes = stream.ToArray();
    };

    // Save content to file object
    PX.SM.FileInfo textDoc = new PX.SM.FileInfo(name, null, bytes);

    if (textDoc != null)
    {
        // Trigger file download
        throw new PXRedirectToFileException(textDoc, true);
    } else {
      //TODO: You could raise an exception here also to notify the user
      PXTrace.WriteInformation("Could not generate file");
    }
}

嗨,Markoan,您的代码帮助我创建了文本文件,但文本文件的内容与数据视图的第一条记录重复。我的记录类型只有三个值“00”表示第一条记录“01”表示第 2 到 n-1 条记录,“99”表示第 n 条记录。

虽然我对你的代码做了一些改动

// We need at least these, listing them for reference
using PX.Data;
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;

// This is your dataview
public PXSelect<MayBankGIRO> Document; 

// Your action delegate
public PXAction<MayBankGiroFilter> createTextFile;

[PXUIField(DisplayName = "Create Text File")]
[PXButton()]
public virtual IEnumerable CreateTextFile(PXAdapter adapter)
{
    // You can use this method to print debug information for your customizations
    // Just remove when you are done testing
    PXTrace.WriteInformation("Generating records");

    // We will build the content as a string list first
    List<string> myList = new List<string> { };

    // If the value of 'ReordType' can change for each record, you don't need this
    MayBankGIRO giroObject = this.Document.Current;

   foreach (MayBankGIRO dacRecord in this.Document.Select())
            {
                // Does 'ReordType' change for each record?
                // if it does you may need to use 'dacRecord.ReordType' in this if instead
                myList.Add(dacRecord.ReordType + "|" + dacRecord.CustomerReferenceNumber + "|" + dacRecord.ClientBatchID + "|" + dacRecord.Country + "|");

            }

    PXTrace.WriteInformation("Generating file");

    // Set the name
    string filename = "DAWN" + ".txt";

    // Use our download method
    Download(myList, filename);
}

// We can define a static method to be able to reuse this later for other DACs
public static void Download(List<string> lines, string name)
{
    var bytes = default(byte[]);

    // Write all lines to stream
    using (MemoryStream stream = new MemoryStream())
    {
        StreamWriter sw = new StreamWriter(stream);
        foreach (string line in lines)
        {
            sw.WriteLine(line);
        }
       // sw.Close(); this was showing some error 

        stream.Position = 0; // "Cannot reach a closed stream" hence i added it in the next line
        bytes = stream.ToArray();
        sw.Close();

    };

    // Save content to file object
    PX.SM.FileInfo textDoc = new PX.SM.FileInfo(name, null, bytes);

    if (textDoc != null)
    {
        // Trigger file download
        throw new PXRedirectToFileException(textDoc, true);
    } else {
      //TODO: You could raise an exception here also to notify the user
      PXTrace.WriteInformation("Could not generate file");
    }
}