我正在使用 C# xamarin forms android,我正在嘗試洗掉MultipartFormDataContent中的專案,但我做不到。我試過這些代碼:
當我嘗試再次上傳 MultipartFormDataContent 時使用 null,它會給我錯誤“System.NullReferenceException:物件參考未設定為物件的實體。”
content = null;
我也嘗試過 Dispose() 但是當我嘗試再次上傳時它給了我這個錯誤“System.ArgumentOutOfRangeException:索引超出范圍。必須是非負數并且小于集合的大小。”:
content.Dispose();
但它不允許我清理我的 MultipartFormDataContent。
這是我的代碼:
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
}
}
我知道,例如,像這樣的串列
List<string> listImgName = new List<string>();
我可以做這樣的事情,它會清理我的清單:
listImgName.Clear();
我怎么能做這樣的事情,但使用 MultipartFormDataContent ?
非常感謝!
uj5u.com熱心網友回復:
解決方案
我無法清理MultipartFormDataContent ,但我可以在我的函式中宣告它。這樣它就毀了自己,我可以再次使用它。所以我這樣做了,它奏效了。我在這里留下了我的代碼:
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();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/507020.html
標籤:C# xamarin xamarin.forms xamarin.android