Indy IdMessageBuilder attachments from memory - delphi-xe7

Is there a way that one can add a related attachment from memory instead of a file saved on disk using Indy's MessageBuilder?
I am adding a footer signature to my emails and the image is saved as a blob field in a database.
I can do this with IdMessage but MessageBuilder just makes everything so easier.
I am using Delphi XE7 and Indy 10.

TIdMessageBuilderAttachment has a public Data: TStream property, and TIdMessageBuilderAttachments has an overloaded Add() method with an AData: TStream parameter.
If a TStream is used for an attachment's data, the builder creates a TIdAttachmentMemory and copies the stream data into it. Otherwise, the builder creates a TIdAttachmentFile and assigns the specified filename to it.
So, you can obtain a TStream for your blob data, such as from TDataSet.CreateBlobStream(), and create an email attachment from that.

Related

How can I serialize and deserialize a private type?

I'm working with the GNAT.OS_Lib module to implement a process manager for Linux. I'm using the subprograms in that module to spawn processes and get back PIDs, and now I want to save those PIDs to a file so they can be retrieved later to kill the process, get its status, etc. The problem is, the Process_Id type is private:
type Process_Id is private; -- s-os_lib.ads, line 743
The module provides a Pid_To_Integer function, but not the inverse. How can I serialize and deserialize Process_Ids?
Study section K.2 "Language Defined Attributes" in the Ada Language Reference Manual http://www.ada-auth.org/standards/12rm/html/RM-K-2.html
The attributes to study are S'Read, S'Write.
Your file must be created as a stream file. The Process_Id'Write attribute will serialize and write to the stream file. The Process_Id'Read attribute will read and de-serialize the data in the file.
If S'Read and S'Write do not work for you because of the nature of a compound type you should use the S'Input and S'Output attributes, which will read and write any bounds or discriminants.
S'Input and S'Output will work correctly with all types.

Why Firebase creates a String before upload when i use "POST" method

Im trying to upload information from Excel vba with JSON to Firebase database
The question is: why method "POST" create a alphanumeric key and place the information inside it?
If i use the "PUT" method everything it saves properly but deletes all previous data.
please see the attached files to see what i mean!
HTTP POST (read the docs) is the equivalent of a "push" in Realtime Database, which creates a random child key where the object will be placed. HTTP PUT (read the docs) is just places the data at the specified key.

Different parameters in Meteor call stubs

I have a meteor method that's supposed to handle file/image uploads by passing a cdn key, which is just a string.
For latency compensation though, I want to add the actual image blob to LocalMongo, that way I can add an image preview.
This is a problem since I want to just pass a string key to my server method, while I want to pass a file blob to my client method stub. Does Meteor support this? I don't want to pass the image blob to my server (as doing so would serialize the blob/make the call costly).
A solution I'm thinking of is to just define two Meteor methods with different names, the first one being for the client and the other for the server, and just calling them both with the proper parameters. Is this the proper way to do this in Meteor?
EDIT: My solution above doesn't actually work because Meteor realizes there is no method on the server (and nukes the local changes of my client method)
Just a suggestion, you can save the file blob in a Session variable and access in the method when the method stub is called from client, like this,
Meteor.methods({
'yourMethod': function (key) {
if (Meteor.isClient) {
var fileBlob = Session.get('my-file-blob'); //set this variable just before calling this method. And don't forget to remove it when template is destroyed.
} else {
}
}
});
Like I said, I didn't test it but just a suggestion. Hope it helps.

How should I store a lookup collection?

I am debugging an application that uses an XML file to store data as key/value pairs.
Every pageload the XML file is parsed to populate a Dictionary collection with these key/value pairs. The Dictionary is then used to look up values based on their keys.
Is this method of loading data from an XML file on every page load a good practice?
Here are some thoughts:
Should the XML data be stored in a database table instead?
Should I work with a collection or hit the database for every lookup request?
Could the collection be loaded on application start and set as a global/application property?
What do you guys think?
I would suggest loading the results of the XML Parsing into the ASP.NET Cache:
Dictionary<string, object> values = new Dictionary<string, object>();
if(Cache["ConfigDictionary"] != null)
{
values = (Dictionary<string, object>)Cache["ConfigDictionary"];
}
else
{
// Load data into the dictionary here
// This dependency will ensure that you don't keep stale data in the cache
CacheDependency dependency =
new CacheDependency(Server.MapPath("yourFileHere.xml"));
// Inser the Dictionary into the cache
Cache.Insert("ConfigDictionary", values, dependency);
}
I agree with #Justin, keeping the collection into cache is nice.
Also, the question about storing data in a database, depends mostly on what content the app is loading. If there is config settings, or another data that has low probability to change, I think is more efficient keeping it in a XML file. Why?
Easier to maintain;
You dont need to care about connections, queries, parameters;
Easier to view data content;
Non database-skilled people (support for app), can change settings.
I would store it in the database instead of XML file. It is easier to query (if you have to run some reports for example) and also backup is sort of more controllable.
Of course, I assume you are using database for your web application in general.
You may try to use Viewstate to decrease number of database runs if your are in the context of the same web page.

Dynamic XML

What's the ideal way of generating XML without creating and saving a file?
I'm thinking of using an ASP.NET page with code behind to generate the markup as XML.
Is this possible? Or would you have an alternative way?
I have a flash component that reads an XML file and I need to dynamically generate this file. I don't have write permission so I won't have the ability to create and save a file.
I was thinking of having an application page that grabs the data and provide property methods to generate the xml on the Settings.xml.aspx page with Settings.xml.aspx.cs codebehind.
Thanks.
The easiest way is to use System.Xml.XmlDocument or System.Xml.Linq.XDocument to build up the document. Both can be streamed out to the Response.OutputStream.
The smoothest approach (especially if you turn off buffering) is simply to create an XmlTextWriter round the Response.OutputStream. This is a forward only approach to generate XML but if the output is large it means you need less memory and content starts to arrive at the client earlier.
System.Xml.XmlDocument ?
In fact there are many ways to do it. It all depends on your needs. Maybe you could have a look at some examples of XDocument (or XmlDocument in .NET 2.0) and XmlWriter, none of these require you to save the XML to a file. You can either keep the object model in memory when using XDocument or write to a MemoryStream when using XmlWriter:
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
using (System.IO.MemoryStream stream = new System.IO.MemoryStream())
{
using (XmlWriter writer = XmlWriter.Create (stream, settings))
{
writer.WriteStartElement ("customer");
writer.WriteElementString ("firstname", "Jim");
writer.WriteElementString ("lastname"," Bo");
writer.WriteEndElement();
}
// do further processing with the stream
}
The difference between the two is basically that the first one gives you access to the DOM whereas the second one simply writes out XML to an underlying stream.
Unfortunately, without knowing more details this question can only be answered vaguely.
Yes, it is perfectly feasible to generate XML "on the fly". Take a look at the XmlDocument class. And more info here.

Resources