Skip to main content
Question

Illegal Characters in path when customize save file in Invoice

  • February 17, 2025
  • 6 replies
  • 52 views

        public delegate void PersistDelegate();
        [PXOverride]
        public void Persist(PersistDelegate baseMethod)
        {
            try
            {
                if (Base.Document.Current != null)
                {
                    ARRegisterExt aRInvoiceExt = PXCache<ARRegister>.GetExtension<ARRegisterExt>(Base.Document.Current);
                    if (aRInvoiceExt != null)
                    {
                        if (aRInvoiceExt.UsrWingAmount == null || aRInvoiceExt.UsrWingAmount != Base.Document.Current.CuryOrigDocAmt)
                        {
                            aRInvoiceExt.UsrWingAmount = Base.Document.Current.CuryOrigDocAmt;
                            //Generation http url
                            var client = new RestClient();
                            string imageUrl = "https://material.angular.io/assets/img/examples/shiba2.jpg";
                            var request = new RestRequest(imageUrl, Method.GET);

                            byte[] data = client.DownloadData(request);
                            PX.SM.FileInfo file = new PX.SM.FileInfo("AR" + DateTime.Now.Year + DateTime.Now.Month + DateTime.Now.Day + DateTime.Now.ToString("HHmmss") + ".jpg", "WingQR", data);
                            var uploadFileMaintenance = PXGraph.CreateInstance<UploadFileMaintenance>();
                            uploadFileMaintenance.SaveFile(file);
                            PXNoteAttribute.AttachFile(Base.Document.Cache, Base.Document.Current, file);
                            aRInvoiceExt.UsrWingUID = file.UID;

                        }
                    }
                }
            }
            catch (PXException ex)
            {
                throw ex;
            }
            baseMethod();
        }

the code above working fine in invoice save. but when I create prepare invoice from shipment, the file link show illegal file path like this

After click on file link it show error

 

6 replies

praveenpo
Semi-Pro II
Forum|alt.badge.img+3
  • Semi-Pro II
  • 99 replies
  • February 17, 2025

Hi ​@vannakheng,
 

Error  Illegal characters in the path issue might cause of below reasons

  1. The image file is missing with the extension. It just says file, no .jpg, gif, etc. Save the Image file with a name and Insert In Email Template
  1. Check is set permissions for the Files attached, try to check Mark Is public access on the File Maintenance and see if that addresses the issue.
  1. Check if the Image contains any Links try to add a Plain Image without any links

Forum|alt.badge.img+8
  • Captain II
  • 366 replies
  • February 17, 2025

Hi ​@vannakheng 

 

It looks like file is saving before the invoice number is generated, illegal characters include “<>” could you maybe move this to override “Remove Hold” or “Release”? This will ensure you have a invoice number in the DB.

 

I believe “\” is also an illegal character.

 

Hope this helps,

Aleks


  • Author
  • Freshman I
  • 8 replies
  • February 17, 2025
aiwan wrote:

Hi ​@vannakheng 

 

It looks like file is saving before the invoice number is generated, illegal characters include “<>” could you maybe move this to override “Remove Hold” or “Release”? This will ensure you have a invoice number in the DB.

 

I believe “\” is also an illegal character.

 

Hope this helps,

Aleks

yes If move code to on release, it’s working fine no problem. But my logic want to generate file when prepare invoice from shipment. Can we change the file path when we save fileinfo.


Forum|alt.badge.img+8
  • Captain II
  • 366 replies
  • February 17, 2025

@vannakheng 

 

If you want to do this when the invoice is prepared, you could try to override the “Prepare Invoice” action and execute your code after the initial Base.Save.Press(); is called, this should ensure that the there is an invoice nbr generated.

 

Aleks


davidnavasardyan
Jr Varsity I
Forum|alt.badge.img+2

Hi ​@vannakheng ,

You’re running into an “Illegal Characters in Path” error because the file name you generate sometimes includes characters that Windows (and other file systems) won’t accept in a path. Even if your DateTime format looks okay, local or regional settings can insert slashes or other characters.

For example:

var client = new RestClient();
var request = new RestRequest("https://material.angular.io/assets/img/examples/shiba2.jpg", Method.GET);

byte[] data = client.DownloadData(request);

// Construct a safe file name using a fixed date/time format
string fileName = $"AR_{DateTime.Now:yyyyMMdd_HHmmss}.jpg";

PX.SM.FileInfo file = new PX.SM.FileInfo(fileName, null, data);

var uploadFileMaintenance = PXGraph.CreateInstance<UploadFileMaintenance>();
uploadFileMaintenance.SaveFile(file);

PXNoteAttribute.AttachFile(Base.Document.Cache, Base.Document.Current, file);

ARRegisterExt aRInvoiceExt = PXCache<ARRegister>.GetExtension<ARRegisterExt>(Base.Document.Current);
aRInvoiceExt.UsrWingUID = file.UID;

This should eliminate the “Illegal Characters in Path”


  • Freshman I
  • 7 replies
  • February 24, 2025

If I were to write something that works always, I would create a method and pass the string I want to verify and return a clean and valid string. This will do the work

private string ReplaceInvalidChars(string validationString)
{
    char[] invalidChars = Path.GetInvalidFileNameChars();
    //If you want to replace the inavalid characters with a valid character like dash
    return new string(validationString.Select(chr => invalidChars.Contains(chr) ? '-' : chr).ToArray());
    //If you want to remove the invalid characters
    return new string(validationString.Where(chr => !invalidChars.Contains(chr)).ToArray());
}

 


Reply


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings