Export Blob to File, what extension?

ta5ta5 Member Posts: 1,164
edited 2015-09-28 in NAV Three Tier
Hi
I want to export the files stored in myTable.myBlob to separate files, from a batch.
The problem is the extension. How can I give the correct file extension to the created files? It could be bmp, jpg etc., so I don't know.
Thx in advance
Thomas

Answers

  • lyngelynge Member Posts: 85
    Hi Thomas

    Most files got magic numbers - a few bytes in the beginning identifying the type of the contents.
    See https://en.wikipedia.org/wiki/Magic_number_(programming)
  • rocatisrocatis Member Posts: 163
    Store the extension in a separate field when you populate the BLOB.
    Brian Rocatis
    Senior NAV Developer
    Elbek & Vejrup
  • ta5ta5 Member Posts: 1,164
    Hi experts
    Thx for your answers. Because there are already data existing, I'll opt for the "Magic Numbers"!
    Regards
    Thomas
  • MarijnMarijn Member Posts: 69
    I have some working code which does this. I'll post it here if I can still find it.
  • MarijnMarijn Member Posts: 69
    What about this? This code can probably be improved in many ways, but it does the job for images and should get you going.

    PROCEDURE GetFileType@1000000005(BlobRef@1000000000 : Record 99008535) : Text[3];
    VAR
    NVInStream@1000000005 : InStream;
    MyChar@1000000004 : Char;
    MyString@1000000003 : Text[10];
    i@1000000002 : Integer;
    Extention@1000000001 : Text[3];
    BEGIN
    //**
    // Function to resolve file extension from the blob contents..
    //*

    BlobRef.Blob.CREATEINSTREAM(NVInStream);

    FOR i := 1 TO 10 DO BEGIN
    NVInStream.READ(MyChar, 1);
    MyString += FORMAT(MyChar);
    END;

    IF COPYSTR(MyString, 7, 4) = 'JFIF' THEN
    Extention := 'jpg'
    ELSE IF COPYSTR(MyString, 2, 3) = 'PNG' THEN
    Extention := 'png'
    ELSE IF COPYSTR(MyString, 1, 3) = 'GIF' THEN
    Extention := 'gif'
    ELSE IF COPYSTR(MyString, 1, 2) = 'BM' THEN
    Extention := 'bmp'
    ELSE
    ERROR('unresolved');

    EXIT(Extention);
    END;
  • ta5ta5 Member Posts: 1,164
    Hi Marijn
    Thx for the code. I'll give it a try!
    Regards
    Thomas
Sign In or Register to comment.