【发布时间】:2020-06-22 07:46:19
【问题描述】:
我有一个包含以下参数的模型
public class FileExchangeDetails
{
public Guid SenderId { get; set; }
public List<Files> Files { get; set; }
public Guid Id { get; set; }
}
public class Files
{
public string FilePath { get; set; }
public string FileName { get; set; }
public byte[] FileBytes { get; set; }
public string FileType { get; set; }
}
我正在使用FileExchangeDetails 发送文件数据(图像或文件)
public static async Task<string> PostFilesToToApi(string apiUrl, FileExchangeDetails exchangeDetails)
{
try
{
using (var httpreqclient = new httpclient())
{
httpreqclient.baseaddress = new uri(string.format(apiurls.weburl, application.current.properties["currentsitename"]));
httpreqclient.defaultrequestheaders.add("authorization", "bearer " + applicationcontext.accesstoken);
httpreqclient.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json"));//accept header
var content = jsonconvert.serializeobject(exchangedetails, formatting.none);
var stringcontentinput = new stringcontent(content, encoding.utf8, "application/json");
var response = await httpreqclient.postasync(new uri(string.format(apiurls.weburl, application.current.properties["currentsitename"]) + apiurl), stringcontentinput);
var stringasync = await response.content.readasstringasync();
if (response.issuccessstatuscode)
{
var responsejson = stringasync;
return responsejson;
}
}
}
catch (Exception exception)
{
LoggingManager.Error(exception, "PostFilesToToApi", "ApiWrapper");
}
return null;
}
功能上一切正常,但现在我需要在上传到服务器时显示文件上传的百分比。
我对 web 客户端几乎没有指导方针,但参数只有字节 [] 或字符串。
如何使用 httpclient 获取该 % 数据。有人可以帮我解决这个问题吗?
【问题讨论】:
标签: file xamarin.forms file-upload