0

I am trying to attach a file to the Xero invoice and using Xero.NetStandard.OAuth2.Api and the net framework starter app for it.

Below is the code we are using to attach, however it throws an error : A validation exception occurred:The file couldn't be uploaded because it isn't a supported file type.

We are uploading and .png type file also tried using a .pdf file


var xeroToken = TokenUtilities.GetStoredToken(); var utcTimeNow = DateTime.UtcNow;

        var serviceProvider = new ServiceCollection().AddHttpClient().BuildServiceProvider();
        var httpClientFactory = serviceProvider.GetService<IHttpClientFactory>();

        XeroConfiguration XeroConfig = new XeroConfiguration
        {
            ClientId = ConfigurationManager.AppSettings["XeroClientId"],
            ClientSecret = ConfigurationManager.AppSettings["XeroClientSecret"],
            CallbackUri = new Uri(ConfigurationManager.AppSettings["XeroCallbackUri"]),
            Scope = ConfigurationManager.AppSettings["XeroScope"],
            State = ConfigurationManager.AppSettings["XeroState"]
        };

        if (utcTimeNow > xeroToken.ExpiresAtUtc)
        {
            var client = new XeroClient(XeroConfig, httpClientFactory);
            xeroToken = (XeroOAuth2Token)await client.RefreshAccessTokenAsync(xeroToken);
            TokenUtilities.StoreToken(xeroToken);
        }

        string accessToken = xeroToken.AccessToken;
        string xeroTenantId = xeroToken.Tenants[0].TenantId.ToString();

        var AccountingApi = new AccountingApi();

        var sevenDaysAgo = DateTime.Now.AddDays(-7).ToString("yyyy, MM, dd");
        var invoicesFilter = "Date >= DateTime(" + sevenDaysAgo + ")";
        var response = await AccountingApi.GetInvoicesAsync(accessToken, xeroTenantId, null, invoicesFilter);

        var invoices = response._Invoices;

        foreach (Invoice inv in invoices)
        {
            if (inv.HasAttachments == false && inv.InvoiceID != null)
            {

                string filePath = ConfigurationManager.AppSettings["AttachmentPath"].ToString();
                filePath = Path.Combine(filePath, inv.InvoiceNumber);
                string[] filePaths = Directory.GetFiles(filePath);
                
                    foreach (var file in filePaths)
                    {
                        byte[] contentFileBytes = System.IO.File.ReadAllBytes(file);

                        Guid invoiceId = Guid.Parse(inv.InvoiceID.ToString());
                        var attachResponse = await AccountingApi.CreateInvoiceAttachmentByFileNameAsync(accessToken, xeroTenantId, invoiceId, System.IO.Path.GetFileNameWithoutExtension(file), true, contentFileBytes);
                    }
                
                
            }
        }

Could anyone please help, if there is any sample code for attaching a file to invoice created.

  • Why do you remove the extension from the filename while submitting it? – droopsnoot Jun 23 '20 at 09:06
  • The parameters on your CreateInvoiceAttachment seem incorrect to me, it looks to me as if it should end invoiceID, filename, body-content, include-online, but you have invoiceID, filename, include-online, body-content. Can't think why it compiles if that is the case, though. – droopsnoot Jun 23 '20 at 12:24

0 Answers0