-2

I'm looking for equivalent function in delphi.

This is the foxpro codes to convert to string and store it to LONGTEXT fields.

xString = strconv(filetostr("C:\AAA.JPG"), 13)

and back

strtofile(strconv(Temp_T.Photo, 14), "C:\BBB.JPG")

Can someone help me to convert lines these please?

This code is one way to store any files to "Photo LONGTEXT NULL" fields

Blue
  • 22,608
  • 7
  • 62
  • 92
  • The first line encodes the file content into a Base64 stream. The second one unencodes (see [FILETOSTR](https://learn.microsoft.com/en-us/previous-versions/visualstudio/foxpro/kzb1hd0e(v%3dvs.80)) and [STRCONV](https://learn.microsoft.com/en-us/previous-versions/visualstudio/foxpro/k0yb2181(v=vs.80)) reference). – Victoria Aug 08 '18 at 23:42
  • 1
    Depending on your Delphi version, there are Base64Encode and Base64Decode functions available. You can probably find implementations of them both with a Google search on either of those function names followed by *delphi* as well. – Ken White Aug 08 '18 at 23:52
  • Additionally you'll find resources about reading and writing images to/from data fields. In the end you may have to write some code to bring them together and have the equivalent function. – Sertac Akyuz Aug 09 '18 at 00:08
  • @SertacAkyuz: Base64 decoding should recreate the image, and `LONGTEXT` is a `TMemoField`, which will store Base64 encoded text like any other text content. There shouldn't be much additional work - it's text. – Ken White Aug 09 '18 at 00:10
  • @Ken. Oh, Ok. What about reading/writing the jpeg from/to disk? It will still require one more piece of work I presume. – Sertac Akyuz Aug 09 '18 at 00:12
  • @Sertac: You would encrypt from disk directly through the function into the field, and decrypting converts back from text directly to binary, which you would write with a stream (TFileStream is probably the easiest). There's almost no work done, especially if the encode/decode functions take a TStream directly. – Ken White Aug 09 '18 at 00:14
  • See [this answer](https://stackoverflow.com/a/30041173/62576) for an example of how to do this in Delphi 2007 and later using Indy (which is installed in Delphi automatically). – Ken White Aug 09 '18 at 00:37
  • The variable xString I can use that in saving to LONGTEXT Field like this. StrSql = "update inv_items set photo = '" + xString + "' where id = 1". – Jojo Imperial Aug 09 '18 at 09:49
  • The whole file content converted to Base64 using STRCONV. Another purpose of this is to avoid using BLOB field because BLOB in MySQL has a problem on restoring the entire BACKUP if there is a BLOB content. So i prefer LONGTEXT that why i using STRCONV in VFP. Now it is in need to convert codes to DELPHI. – Jojo Imperial Aug 09 '18 at 09:57
  • 1
    Unfortunately this site is not a substitute for a code writing or code conversion service. You have to put together the information you have. – Sertac Akyuz Aug 09 '18 at 18:24
  • I gave you a direct link to an example to get you started four comments above this one. It's your job to take that example (which clearly demonstrates use of the two functions I mentioned) and adapt them to your requirements. Once you've made an effort to do so and run into a problem, you can create a new question here, explain the problem you've encountered, include your code, and ask a specific questions related to that code, and we can try to help. – Ken White Aug 10 '18 at 00:13

1 Answers1

-1

If you want to read and write with files, the IoUtils unit has some handy functions. If you're working with JPG it's probably best not to convert to string. Better just store the raw bytes instead.

uses System.IoUtils;

var 
  xBytes:TBytes;
begin
  xBytes  := TFile.ReadAllBytes('C:\AAA.jpg');
  TFile.WriteAllBytes('C:\BBB.JPG',xBytes);
end;
Wouter van Nifterick
  • 23,603
  • 7
  • 78
  • 122
  • I think this is about saving to and reading from a FoxPro BLOB field topic. – Victoria Aug 08 '18 at 23:52
  • This is about Base64 encoding of binary data to store in a text field, not about reading/writing to files. – Ken White Aug 08 '18 at 23:54
  • Actually, the more I think about it the worse this answer gets. Using what you've written, there's no need to read or write the file at all. You can just copy the file. I think you've badly misread the question asked. – Ken White Aug 09 '18 at 00:32
  • 1
    This is all about converting file content to string. The variable xString I can use that in saving to LONGTEXT Field like this. StrSql = "update inv_items set photo = '" + xString + "' where id = 1". – Jojo Imperial Aug 09 '18 at 10:05
  • No BLOB here it is all LONGTEXT because BLOB field sometime has a problem on RESTORING MySQL backup with BLOB fields. That's in me it is better to use LONGTEXT. – Jojo Imperial Aug 09 '18 at 10:10
  • Any file not only JPG. – Jojo Imperial Aug 09 '18 at 10:13
  • @KenWhite: This just demonstrates how to read bytes from disk and how to write it back. That's part of what Jojo is trying to achieve. Of course executing these two lines consecutively makes no sense. :-) – Wouter van Nifterick Aug 09 '18 at 23:52
  • Except that's not part of what the poster asked at all. The poster asked about the equivalent of the StrConv function, which transforms data (in this case, to and from Base-64). The inclusion of file names isn't relevant other than the function requiring them in FoxPro. – Ken White Aug 10 '18 at 00:10
  • @kenWhite. Meh, different interpretation. Jojo asks explicitly for two lines of code to be ported to Delphi, which involve file IO. That's the part that I've tried to helped out with. Combined with your base-64 comments, I think JoJo is almost done. – Wouter van Nifterick Aug 10 '18 at 01:11