Today in this post we will discuss about using Stream to Export or Import Files or in other words File handling using Streams.
First Example I am discussing about is how to Import Image to Item Picture.
First part of the code checks for if any existing Picture in the records Picture field, here we use Count to check, because Picture field is defined as MediaSet can hold multiple picture, so giving warning for Overwrite.
In Second part we are Uploading the Selected Image to the InStream.
Finally we clear the Picture field and load Picture from InStream, here file name will be used as description for the image. And save the record post importing of the Image.
Below code sequence is self explanatory.
In Second example we are Exporting the image from the record, any/all images stored in Picture field of the Item record.
Again we are testing with Count if any Image stored, else flag the Error nothing to Export as record don’t have any picture stored.
As told above since it is MediasSet type so possibility of more than one Image may have been stored, so we setup for loop to access all of them.
In TenantMedia table all Images are stored with unique GUID, so we get them each by Item(index).
Calcfields used to load the content in memory and final checking if it HASVALUE.
We build the filename as Item_Image1.ext, we will look into GetImgFileExt function below.
Next we load the Image in InStream.
Finally we save the Image on disk by specified filename.
This below function we use to find right extension of the file from TenantMedia field Mime Type.
In last and below function we are using TempBlob Table and Stream to handle file.
We create the OutStream of BlobTable Blob Field, set it as destination for my Export XML Port.
Further creating the InStream we are transferring the content of Blob to the File.
Hope you will start using this technique in your future projects, when working with File Handling.
We will discuss on other concepts in our upcoming posts.
Not sure if this is my Last post for this year, so wishing you Happy New Year 2019 in advance.
Keep practicing and learning, see you again in my next post.
Till then bye, take good care of your self and remain safe.