Related to the topic, I have the following code, but this time I try to obtain the pdf from an api, and save it in Shipment, I show you the code and the error that appears:
[PXButton(CommitChanges = true)]
[PXUIField(DisplayName = "Track")]
protected void apiTracking()
{
DownloadAndUploadFile();
}
public void DownloadAndUploadFile()
{
try
{
using (HttpClient client = new HttpClient())
{
string url = "http://qaglp.paquetexpress.mx:8083/wsReportPaquetexpress/GenCartaPorte?trackingNoGen=19167736449";
HttpResponseMessage queryResult = client.GetAsync(url).Result;
if (queryResult.IsSuccessStatusCode)
{
byte[] fileContent = queryResult.Content.ReadAsByteArrayAsync().Result;
UploadFile(fileContent, "downloadedFile55.pdf");
}
else
{
PXTrace.WriteWarning("HTTP request was not successful.");
}
}
}
catch (Exception ex)
{
PXTrace.WriteWarning("An exception occurred: " + ex.Message);
}
}
private void UploadFile(byte[] file, string fileName)
{
try
{
// Create an instance of PX.SM.UploadFileMaintenance
PX.SM.UploadFileMaintenance uploadFileMaintenanceGraph = PXGraph.CreateInstance<PX.SM.UploadFileMaintenance>();
// Create a FileInfo object for the file
PX.SM.FileInfo fileInfo = new PX.SM.FileInfo(fileName, null, file);
// Save the file
if (uploadFileMaintenanceGraph.SaveFile(fileInfo, PX.SM.FileExistsAction.CreateVersion))
{
PXTrace.WriteInformation("File uploaded successfully.");
// Associate the file with each shipment
foreach (SOShipment shipment in Base.Document.Select().RowCast<SOShipment>().ToList())
{
// Upload the file and associate it with the shipment
uploadFileMaintenanceGraph.SaveFile(fileInfo, PX.SM.FileExistsAction.CreateVersion);
uploadFileMaintenanceGraph.AttachFile(typeof(SOShipment).FullName, shipment.ShipmentNbr, fileInfo);
}
}
else
{
PXTrace.WriteWarning("Error uploading the file.");
}
}
catch (Exception ex)
{
PXTrace.WriteWarning("An exception occurred: " + ex.Message);
}
}
and I get this error:
[2024-03-05 18:21:11.673] \App_RuntimeCode\SOShipmentEntry.cs(164): error CS1061: 'UploadFileMaintenance' does not contain a definition for 'AttachFile' and no accessible extension method 'AttachFile' accepting a first argument of type 'UploadFileMaintenance' could be found (are you missing a using directive or an assembly reference?)