Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/crossgeeks/fileuploaderplugin

Simple cross platform plugin to upload files.
https://github.com/crossgeeks/fileuploaderplugin

file multipart plugin upload xamarin

Last synced: 1 day ago
JSON representation

Simple cross platform plugin to upload files.

Awesome Lists containing this project

README

        

## FileUploader Plugin for Xamarin iOS, Android, UWP, Mac, tvOS and watchOS
Simple cross platform plugin for file multipart uploads.


     

## Features

- Multipart file uploading with headers and parameters
- Upload multiple files at once
- Upload request progress feedback
- Upload files by using bytes or file path
- Set custom boundary

### Setup
* Available on NuGet: http://www.nuget.org/packages/Plugin.FileUploader [![NuGet](https://img.shields.io/nuget/v/Plugin.FileUploader.svg?label=NuGet)](https://www.nuget.org/packages/Plugin.FileUploader/)
* Install into your PCL project and Client projects.

**Platform Support**

|Platform|Version|
| ------------------- | :------------------: |
|Xamarin.iOS|iOS 7+|
|Xamarin.Android|API 15+|
|Windows 10 UWP|10+|
|Xamarin.Mac|10.9+|
|watchOS|2.0+|
|tvOS|9.0+|

### API Usage

Call **CrossFileUploader.Current** from any project or PCL to gain access to APIs.

You can upload a file using the file path or bytes.

**FilePathItem**
```csharp
///
/// Path: File path location.
/// FieldName: Request field name for the file to be uploaded
///
public class FilePathItem
{
public string Path { get; }
public string FieldName {get; }
}
```

**FileBytesItem**
```csharp
///
/// FieldName: Request field name for the file to be uploaded
/// Bytes: File bytes.
/// Name: Name of the file.
///
public class FileBytesItem
{
public string Name { get; }
public string FieldName { get; }
public byte[] Bytes { get; }
}
```

**UploadFileAsync**
```csharp
///
/// Upload file using file path
///
/// Url for file uploading
/// File path item to be uploaded
/// Request headers
/// Additional parameters for upload request
/// Custom part boundary
/// FileUploadResponse
Task UploadFileAsync(string url, FilePathItem fileItem, IDictionary headers =null,IDictionary < string, string> parameters = null, string boundary = null);

///
/// Upload files using file path
///
/// Url for file uploading
/// File path items to be uploaded
/// Tag reference of the upload request
/// Request headers
/// Additional parameters for upload request
/// Custom part boundary
/// FileUploadResponse
Task UploadFileAsync(string url, FilePathItem[] fileItems,string tag, IDictionary headers = null, IDictionary parameters = null, string boundary = null);

///
/// Upload file using file bytes
///
/// Url for file uploading
/// File bytes item to be uploaded
/// Request headers
/// Additional parameters for upload request
/// Custom part boundary
/// FileUploadResponse
Task UploadFileAsync(string url, FileBytesItem fileItem, IDictionary headers = null, IDictionary parameters = null, string boundary = null);

///
/// Upload files using file bytes
///
/// Url for file uploading
/// File bytes of items to be uploaded
/// Tag reference of upload request
/// Request headers
/// Additional parameters for upload request
/// Custom part boundary
/// FileUploadResponse
Task UploadFileAsync(string url, FileBytesItem[] fileItems,string tag, IDictionary headers = null, IDictionary parameters = null,string boundary = null);
```

Usage sample:

Uploading from a file path
```csharp

CrossFileUploader.Current.UploadFileAsync("", new FilePathItem("",""), new Dictionary()
{
{"" , ""}
}
);

```

Uploading from a file bytes
```csharp

CrossFileUploader.Current.UploadFileAsync("", new FileBytesItem("","",""), new Dictionary()
{
{"" , ""}
}
);

```
Uploading multiple files at once
```csharp

CrossFileUploader.Current.UploadFileAsync("", new FilePathItem[]{
new FilePathItem("file",path1),
new FilePathItem("file",path2),
new FilePathItem("file",path3)
},"Upload Tag 1");

```
#### Events in FileUploader
When any file upload completed/failed you can register for an event to fire:
```csharp
///
/// Event handler when file is upload completes succesfully
///
event EventHandler FileUploadCompleted;
```

```csharp
///
/// Event handler when file is upload fails
///
event EventHandler FileUploadError;
```

```csharp
///
/// Event handler when file upload is in progress, indicates what's the upload progress so far
///
event EventHandler FileUploadProgress;
```

For events **FileUploadCompleted** and **FileUploadError** you will get a FileUploadResponse with the status and response message:

```csharp
public class FileUploadResponse
{
public string Tag { get; }
public string Message { get; }
public int StatusCode { get; }
public IReadOnlyDictionary Headers { get; }
}
```

Usage sample:
```csharp

CrossFileUploader.Current.FileUploadCompleted += (sender, response) =>
{
System.Diagnostics.Debug.WriteLine($"{response.StatusCode} - {response.Message}");
};

CrossFileUploader.Current.UploadFileAsync($"",new FileItem("",""));

```

While upload is in progress you can get feedback on event **FileUploadProgress**

You will get a FileUploadProgress with the total bytes sent, total request byte length and progress percentage

```csharp
public class FileUploadProgress
{
public long TotalBytesSent { get; }
public long TotalLength { get; }
public double Percentage { get; }
public string Tag { get; }

}
```

Usage sample:
```csharp
CrossFileUploader.Current.FileUploadProgress += (sender, uploadProgress) =>
{
System.Diagnostics.Debug.WriteLine($"{uploadProgress.Tag} - {uploadProgress.TotalBytesSent} - {uploadProgress.Percentage}");
};
```
### **IMPORTANT**

### iOS:
On AppDelegate.cs

```csharp
/**
* 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;
}
```

Also consider on iOS 9+, your URL must be secured or you have to add the domain to the list of exceptions. See https://github.com/codepath/ios_guides/wiki/App-Transport-Security

### Android:

There are some android specific static properties to specify timeout information:

```csharp
public static TimeUnit UploadTimeoutUnit { get; set; } = TimeUnit.Minutes;
public static long SocketUploadTimeout { get; set; } = 5;
public static long ConnectUploadTimeout { get; set; } = 5;
```

Above you can see the default values. But you can change the value for the timeouts and unit by setting it from your Android project(Could be on MainActivity) like this:

```csharp
FileUploadManager.UploadTimeoutUnit = TimeUnit.Minutes;
FileUploadManager.SocketUploadTimeout = 10;
FileUploadManager.ConnectUploadTimeout = 5;
```

#### Contributors

* [Rendy Del Rosario](https://github.com/rdelrosario)
* [Charlin Agramonte](https://github.com/char0394)
* [Angel Andres Mañon](https://github.com/AngelAndresM)