Vincent
Vincent

Reputation: 3304

How to post multipart/form-data in uwp using flurl

I'm using flurl in uwp, and want to post an image file to the server.

The server api needs a parameter "image_file", a binary file, need to post using multipart/form-data.

Now I have a StorageFile, how to post it?

string res = "";
        try
        {
            res = await _detect_api_url
                   .PostMultipartAsync(mp => mp
                   .AddStringParts(new { api_key = _api_key, api_secret = _api_secret, return_landmark = returnLandmarks, return_attributes = returnAttributes })
                   .AddFile("image_file", imageFile)
                   ).ReceiveString();
        }
        catch (FlurlHttpTimeoutException)
        {
            Debug.WriteLine("FlurlHttp internal time out.");
            res = "FlurlHttp internal time out.";
        }
        catch (FlurlHttpException ex)
        {
            Debug.WriteLine(ex.Message);
            res = ex.Call.ErrorResponseBody;
        }

Upvotes: 1

Views: 1801

Answers (1)

Sunteen Wu
Sunteen Wu

Reputation: 10627

Now I have a StorageFile, how to post it?

The AddFile has two overload methods:

public CapturedMultipartContent AddFile(string name, Stream stream, string fileName, string mediaType = null, int bufferSize = 4096);
public CapturedMultipartContent AddFile(string name, string path, string mediaType = null, int bufferSize = 4096);

So that you can provide a file stream or the file local path for the method to post file to the server. For how to read the file stream from the StorageFile please reference this article. And for the local file path you can directly get it by StorageFile.Path property. Pay attention that you cannot directly provide a local path build by yourself, you must get it from a StorageFile object, details about this please reference File access permissions.

Following is the sample code for posting a file by AddFile methods.

string _detect_api_url = "http://localhost/BackgroundTransferSample/Upload.aspx";
private async void btntest_Click(object sender, RoutedEventArgs e)
{
    StorageFile imageFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/picture0.png"));
    IBuffer buffer = await Windows.Storage.FileIO.ReadBufferAsync(imageFile);
    string res = "";
    try
    {
        //AddFile by file stream.
        res = await _detect_api_url
               .PostMultipartAsync(mp => mp
               .AddFile("image_file", buffer.AsStream(), "imagefile.png", null, (int)buffer.Length)
               ).ReceiveString();

        //AddFile by local file path.
        //res = await _detect_api_url
        //     .PostMultipartAsync(mp => mp
        //     .AddFile("image_file", imageFile.Path)
        //     ).ReceiveString();
    }
    catch (FlurlHttpTimeoutException)
    {
        Debug.WriteLine("FlurlHttp internal time out.");
        res = "FlurlHttp internal time out.";
    }
    catch (FlurlHttpException ex)
    {
        Debug.WriteLine(ex.Message);
        res = ex.Call.ErrorResponseBody;
    }
}

Upvotes: 2

Related Questions