Thursday, August 11, 2016

Usage of File::SendFileToTempStore method in Ax7

In Previous post, I have shown converting document directly to stream and using same. In this, after converting, saving that stream to temporary storage and then from there downloading data and using same. This to show how to use "File::SendFileToTempStore" method.

class Jobs
{
    public static void main(Args _args)
    {
        str                     imageToSave = "C:\\invoice.jpg";
        PurchTable              purchaseTable;
        System.Net.WebClient    webClient;
 System.IO.MemoryStream  memstream;
        System.IO.Stream        stream;

        if (imageToSave && System.IO.File::Exists(imageToSave))
        {
            using (System.IO.FileStream fileStream = new System.IO.FileStream(imageToSave, System.IO.FileMode::Open, System.IO.FileAccess::Read))
            {
                stream = fileStream;
                stream.Seek(0, System.IO.SeekOrigin::Begin);
                str downloadUrl = File::SendFileToTempStore(stream, imageToSave);
                //br.navigate(downloadUrl);

                InteropPermission perm = new InteropPermission(InteropKind::ClrInterop);
                perm.assert();
               
                // BP Deviation Documented
                webClient = new System.Net.WebClient();
                // BP Deviation Documented
                memstream = new System.IO.MemoryStream(webClient.DownloadData(downloadUrl));

  select purchaseTable where purchaseTable.PurchId == "000006"; // select record to which you want to attach
                DocuRef docuref = DocumentManagement::attachFile(purchaseTable.TableId, purchaseTable.RecId, purchaseTable.DataAreaId,DocuType::typeFile(),memstream, System.IO.Path::GetFileName(imageToSave), System.Web.MimeMapping::GetMimeMapping(imageToSave), System.IO.Path::GetFileName(imageToSave));
                // download/view the attachment
                str displayUrl = DocumentManagement::getAttachmentPublicUrl(docuref);
                Browser br = new Browser();
                br.navigate(displayUrl);
                CodeAccessPermission::revertAssert();
            }
        }
    }

}

2 comments:

  1. Very interesting subject, I enjoyed reading it. Everyone should Read this tip to make their life smarter, better, faster and wiser.

    Hire Microsoft Dynamics AX Developer

    ReplyDelete
  2. This was a major help DocumentManagement::attachFile is something I havent used before.

    ReplyDelete