In Delphi, C++Builder and RAD Studio XE6, we integrate with leading Backend as a Service (BaaS) providers to add functionality and platform services to your applications. With BaaS, you get easy access to common services in the cloud without having to build or maintain the backend services yourself. This includes support for being able to upload images to your BaaS account using RAD Studio XE6.
Today, I thought I would create a demo that shows you how to load an image from the gallery, upload it to your Parse.com account (you will need to sign up for an account), display the image URL in an Edit control and then download the uploaded image and display it in your app.
I used the following BaaS components:
- TBackendFiles
- TParseProvider (you could also use TKinveyProvider)
Here is the Object Pascal Code for my application:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
unit ImageUploadBaaS_OP;<br> <br> interface<br> <br> uses<br> System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,<br> FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, IPPeerClient,<br> REST.Backend.ServiceTypes, REST.Backend.MetaTypes,<br> REST.Backend.KinveyServices, System.JSON, REST.OpenSSL,<br> REST.Backend.KinveyProvider, REST.Backend.Providers,<br> REST.Backend.ServiceComponents, FMX.Objects, FMX.StdCtrls, FMX.Edit, REST.Client,<br> FMX.TabControl, System.Actions, FMX.ActnList, FMX.StdActns,<br> FMX.MediaLibrary.Actions, REST.Backend.ParseServices,<br> REST.Backend.ParseProvider, FMX.ListBox, FMX.Layouts;<br> <br> type<br> TForm1 = class(TForm)<br> BackendFiles1: TBackendFiles;<br> btnUploadImage: TButton;<br> Image1: TImage;<br> Image2: TImage;<br> btnDownloadImage: TButton;<br> Edit1: TEdit;<br> ActionList1: TActionList;<br> TakePhotoFromCameraAction1: TTakePhotoFromCameraAction;<br> TakePhotoFromLibraryAction1: TTakePhotoFromLibraryAction;<br> ParseProvider1: TParseProvider;<br> ToolBar2: TToolBar;<br> Label2: TLabel;<br> ListBox1: TListBox;<br> ListBoxGroupHeader1: TListBoxGroupHeader;<br> ListBoxItem1: TListBoxItem;<br> ListBoxItem2: TListBoxItem;<br> imagedownload: TListBoxGroupHeader;<br> ListBoxItem3: TListBoxItem;<br> btnAccessGallery: TButton;<br> procedure btnUploadImageClick(Sender: TObject);<br> procedure btnDownloadImageClick(Sender: TObject);<br> procedure TakePhotoFromLibraryAction1DidFinishTaking(Image: TBitmap);<br> procedure Image1Click(Sender: TObject);<br> procedure Image2Click(Sender: TObject);<br> private<br> function SaveImage: TStream;<br> procedure DownloadImage(const AUrl: string);<br> { Private declarations }<br> public<br> { Public declarations }<br> end;<br> <br> var<br> Form1: TForm1;<br> <br> implementation<br> <br> {$R *.fmx}<br> <br> procedure TForm1.btnUploadImageClick(Sender: TObject);<br> var<br> LStream: TStream;<br> LFile: TBackendEntityValue;<br> begin<br> LStream := SaveImage;<br> try<br> BackEndFiles1.Files.UploadFile('mypicture3.png',LStream, 'image/png',LFile);<br> ShowMessage('Image has been uploaded');<br> Edit1.Text := LFile.DownloadURL;<br> finally<br> LStream.Free;<br> end;<br> end;<br> <br> procedure TForm1.DownloadImage(const AUrl: string);<br> var<br> LStream: TMemoryStream;<br> begin<br> LStream := TMemoryStream.Create;<br> try<br> REST.Client.TDownloadURL.DownloadRawBytes(AUrl, LStream);<br> Image2.Bitmap.LoadFromStream(LStream);<br> finally<br> LStream.Free;<br> end;<br> end;<br> <br> procedure TForm1.Image1Click(Sender: TObject);<br> begin<br> TakePhotoFromLibraryAction1.ExecuteTarget(btnAccessGallery);<br> end;<br> <br> <br> procedure TForm1.Image2Click(Sender: TObject);<br> begin<br> DownloadImage(Edit1.Text);<br> end;<br> <br> procedure TForm1.btnDownloadImageClick(Sender: TObject);<br> begin<br> DownloadImage(Edit1.Text);<br> end;<br> <br> function TForm1.SaveImage: TStream;<br> begin<br> Result := nil;<br> begin<br> Result := TMemoryStream.Create;<br> try<br> Image1.Bitmap.SaveToStream(Result);<br> except<br> Result.Free;<br> raise;<br> end;<br> end;<br> end;<br> <br> procedure TForm1.TakePhotoFromLibraryAction1DidFinishTaking(Image: TBitmap);<br> begin<br> Image1.Bitmap.Assign(Image);<br> end;<br> <br> end. |
Note: This requires XE6 Update 1 to be installed.
Design. Code. Compile. Deploy.
Start Free Trial Upgrade Today
Free Delphi Community Edition Free C++Builder Community Edition
I’ve read your blog, it was very interesting. I’ve found very little about the ‘Bass Client’ components in Embarcadero. I’m using Delphi 11, and I’ve managed to upload files from a VCL application to Back4App, but I haven’t been able to manipulate the ACL for security purposes. Could you guide me on where to find the Delphi documentation for these components?
I think these articles may help:
https://github.com/JordiCorbilla/BaaSDelphiSamples
https://docwiki.embarcadero.com/Libraries/Athens/en/REST.Backend.KinveyProvider.TKinveyProvider