Many times in our applications we need to upload files as fast as we can. So the best way to achieve this is using each platform’s own native apis which will speed up considerably our requests.
Recently, I just released a plugin for uploading files by doing a multipart request FileUploader Plugin for Xamarin and Windows
To start using it install the plugin in your native projects and PCL if applies.
Usage
You can upload files using the path or bytes. Also, can add headers and parameters for the multipart request. (Headers and parameters are optional in case you don’t need either of them for your request).
Upload file using file path
CrossFileUploader.Current.UploadFileAsync("<URL HERE>", new FilePathItem("<REQUEST FIELD NAME HERE>","<FILE PATH HERE>"), new Dictionary<string, string>()
{
{"<HEADER KEY HERE>" , "<HEADER VALUE HERE>"}
}
);
Upload file using file bytes
CrossFileUploader.Current.UploadFileAsync("<URL HERE>", new FileBytesItem("<REQUEST FIELD NAME HERE>","<FILE BYTES HERE>","<FILE NAME HERE>"), new Dictionary<string, string>()
{
{"<HEADER KEY HERE>" , "<HEADER VALUE HERE>"}
}
);
iOS
In order to allow receiving feedback of upload completion when application on background, you should do the following on the AppDelegate.cs
/**
* Save the completion-handler we get when the app opens from the background.
* This method informs iOS that the app has finished all internal processing and can sleep again.
*/
public override void HandleEventsForBackgroundUrl(UIApplication application, string sessionIdentifier, Action completionHandler)
{
FileUploadManager.UrlSessionCompletion = completionHandler;
}
Roadmap
There is still some work to do:
- Android background uploading support
- Binary upload support
- Handling File Upload Queue
- FTP Upload support
For more information look at the repository here: https://github.com/CrossGeeks/FileUploaderPlugin
Hope this helps someone.
Happy coding!
The post Plugin for uploading files in Xamarin and Windows appeared first on Xamboy.