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

问题描述 投票:0回答:1

我正试图生成一个 文本文件 在一个动作按钮上,用图形类中创建的数据视图的内容,并将文件保存在我的本地驱动器上。但我无法做到这一点。

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

我正在使用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.


acumatica
1个回答
1
投票

我利用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");
    }
}

0
投票

假设你的 Document 对象是您需要的视图,其 Select() 方法返回所有你需要的数据记录在你的文件中,这应该是工作。

// 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");
    }
}

0
投票

嗨,Markoan,你的代码帮助我创建了文本文件,但是文本文件的内容与数据视图的第一条记录重复了。我的记录类型只有三个值,"00 "代表第一条记录,"01 "代表第二条到第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");
    }
}


© www.soinside.com 2019 - 2024. All rights reserved.