Home > Software design >  How to clear a MultipartFormDataContent in C# xamarin forms android?
How to clear a MultipartFormDataContent in C# xamarin forms android?

Time:09-07

I'm working with C# xamarin forms android and I'm trying to remove the items that are inside of my MultipartFormDataContent but I can't do it. I've tried these codes:

Using null when I try to upload again the MultipartFormDataContent it gives me the error "System.NullReferenceException: Object reference not set to an instance of an object."

content = null;

Also i've tried Dispose() but it gaves me this error when I try to upload again "System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection.":

content.Dispose();

But It doesn't let me clean my MultipartFormDataContent.

this is my code:

    MultipartFormDataContent content = new MultipartFormDataContent();
    List<StreamContent> listStreamContent = new List<StreamContent>();
    private async void sendImages()
    {
        for (int x = 0; x < listStreamContent.Count; x  )
        {
           content.Add(listStreamContent[x], "file", listFullPathImg[x]);
        }
        var httpClient = new HttpClient();
        var response = await httpClient.PostAsync(url, content);
        string res = await response.Content.ReadAsStringAsync();
        await DisplayAlert("Result", res, "okey");
        for (int x = 0; x < listFullPathImg.Count; x  )
        {
           File.Delete(listFullPathImg[x]);
        }
        clearMultipartFormDataContent();
    }

    public void clearMultipartFormDataContent()
    {
        try
        {
            listStreamContent.Clear();
            content.Dispose();

            //content = null;
        }
        catch (Exception ex)
        {
            // handler exception
        }
    }

I know that for example, with a list like this

List<string> listImgName = new List<string>();

I can do something like this and it clean my list:

listImgName.Clear();

How could I do something like that but with a MultipartFormDataContent ?

Thank you very much!

CodePudding user response:

SOLUTION

I cannot clean my MultipartFormDataContent but I can declare it inside of my function. And that way it destroyed itself and I can use it again. So I did this and it worked. I left my code here:

List<StreamContent> listStreamContent = new List<StreamContent>();
private async void sendImages()
{
    MultipartFormDataContent content = new MultipartFormDataContent();
    for (int x = 0; x < listStreamContent.Count; x  )
    {
       content.Add(listStreamContent[x], "file", listFullPathImg[x]);
    }
    var httpClient = new HttpClient();
    var response = await httpClient.PostAsync(url, content);
    string res = await response.Content.ReadAsStringAsync();
    await DisplayAlert("Result", res, "okey");
    for (int x = 0; x < listFullPathImg.Count; x  )
    {
       File.Delete(listFullPathImg[x]);
    }
    listStreamContent.Clear();
}
  • Related