Uploads
Uploading¶
It's quite common that we want to send something to our server, it can be a new leaderboard entry, a GraphQL query, a still image or audio/video streams. While they are all have common properties, they might need special care on the client side. How a server expects the uploaded data? Url-encoded, multipart/form-data, json text, raw bytes? It's easy to get lost.
The plugin supports the following ways to upload data:
Upload method | What class to use |
---|---|
Raw bytes | System.IO.MemoryStream |
Raw file | System.IO.FileStream |
url-encoded form | Best.HTTP.Request.Upload.Forms.UrlEncodedStream |
multipart/form-data | Best.HTTP.Request.Upload.Forms.MultipartFormDataStream |
Json | Best.HTTP.Request.Upload.JSonDataStream<T> |
Data generated on-the-fly | Best.HTTP.Request.Upload.DynamicUploadStream |
The plugin's versatile UploadStreamBase
class also makes it easy to add new ones.
Plugin provided streams under the Best.HTTP.Request.Upload
namespace always set the "Content-Type"
header to the appropriate value, it's not advised to set them directly!
Upload Raw Bytes¶
This example uses MemoryStream to send pre-generated memory to the server.
We can send textual data the same way, but we have to convert it first to raw bytes:
Upload a File¶
By using a FileStream
, we can send even gigabytes sized files without allocating memory larger then a few dozen kilobytes (1).
- The plugin also pools its internal buffers for maximum memory efficiency!
You only have to set the header and get a stream for the file:
The plugin takes care of stream, it will read only the amount it can send and will dispose the stream too when finished.
Upload an url-encoded
form¶
To upload an url-encoded
form, the plugin provides an easy to use class, the UrlEncodedStream
from the Best.HTTP.Request.Upload.Forms
namespace.
Here's an example how you can use it:
While the UrlEncodedStream
accepts and uploads binary data as a Base64 string, for these cases i suggest to use the MultipartFormDataStream
(1).
- Of course, the server must accept
multipart/form-data
as the upload method in order to useMultipartFormDataStream
!
Upload an multipart/form-data
form¶
From the same namespace (Best.HTTP.Request.Upload.Forms
) as the UrlEncodedStream
, we can use MultipartFormDataStream
for multipart forms:
For textual values we can use the AddField
function. It can accept an System.Text.Encoding
instance to convert the value with that one and use its WebName
property to set the fields mime-type. If no encoding is specified, MultipartFormDataStream
falls back to use System.Text.Encoding.UTF8
.
To send binary data the AddStreamField
function can be used. It acceps a System.IO.Stream
implementation, so practically it can upload any kind of streams.
Upload an object as Json text¶
With the help of the JSonDataStream<T>
class from the Best.HTTP.Request.Upload
namespace, it's easy to upload an object to the server.
Warning
JSonDataStream
does NOT convert the object to JSon right there when created, instead it keeps a reference to it until the remote server is ready to accept the data. This means that any change to the object will be reflected in the sent data too.
Because the Json serialization happens on the sending thread, it can efficiently send larger objects too!
This class uses LitJson, bundled with the plugin.
Uploading on-the-fly generated data¶
Whether you're dealing with on-the-fly audio samples, video chunks, or any other data that doesn't follow a predictable size or timing, DynamicUploadStream
ensures a smooth and uninterrupted uploading process while it still remains easy to use.
It's also good if you getting lots of data quickly sometimes and slower other times or users uploading things at random. It keeps sending your data even if there's a short pause. When you're done sending, just use the Complete()
function.
As you'll see, handling even the most advanced scenarios is as straightforward as creating a stream and writing data into it:
While generating the data, it's possible to check the amount of buffered, unsent bytes in the stream using the BufferedLength
property.
You can start buffering up data in the stream while the request is connecting to the server.
Progress Tracking¶
No matter how UploadStream is utilised or even used directly, progress tracking is an independent feature. Subscribing to progress events can be done by setting an event handler for UploadSettings.OnUploadProgress
: