File 如何获取上载到服务器的%数据
我有一个由以下参数组成的模型File 如何获取上载到服务器的%数据,file,xamarin.forms,file-upload,File,Xamarin.forms,File Upload,我有一个由以下参数组成的模型 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; } publ
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; }
}
功能上一切正常,但现在我需要显示上传到服务器的文件的百分比
对于web客户端,我没有什么指导原则,但对于这一点,参数仅为byte[]或string
如何使用httpclient获取该%数据。有人能帮我吗。你可以创建一个自定义的HttpContent如下
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;
}
internal class ProgressableStreamContent : HttpContent
{
private const int defaultBufferSize = 4096;
private Stream content;
private int bufferSize;
private bool contentConsumed;
private Download downloader;
public ProgressableStreamContent(Stream content, Download downloader) : this(content, defaultBufferSize, downloader) {}
public ProgressableStreamContent(Stream content, int bufferSize, Download downloader)
{
if(content == null)
{
throw new ArgumentNullException("content");
}
if(bufferSize <= 0)
{
throw new ArgumentOutOfRangeException("bufferSize");
}
this.content = content;
this.bufferSize = bufferSize;
this.downloader = downloader;
}
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
Contract.Assert(stream != null);
PrepareContent();
return Task.Run(() =>
{
var buffer = new Byte[this.bufferSize];
var size = content.Length;
var uploaded = 0;
downloader.ChangeState(DownloadState.PendingUpload);
using(content) while(true)
{
var length = content.Read(buffer, 0, buffer.Length);
if(length <= 0) break;
downloader.Uploaded = uploaded += length;
stream.Write(buffer, 0, length);
downloader.ChangeState(DownloadState.Uploading);
}
downloader.ChangeState(DownloadState.PendingResponse);
});
}
protected override bool TryComputeLength(out long length)
{
length = content.Length;
return true;
}
protected override void Dispose(bool disposing)
{
if(disposing)
{
content.Dispose();
}
base.Dispose(disposing);
}
private void PrepareContent()
{
if(contentConsumed)
{
// If the content needs to be written to a target stream a 2nd time, then the stream must support
// seeking (e.g. a FileStream), otherwise the stream can't be copied a second time to a target
// stream (e.g. a NetworkStream).
if(content.CanSeek)
{
content.Position = 0;
}
else
{
throw new InvalidOperationException("SR.net_http_content_stream_already_read");
}
}
contentConsumed = true;
}
}
内部类ProgressableStreamContent:HttpContent
{
private const int defaultBufferSize=4096;
私有流内容;
私有int缓冲区大小;
私人住宅;
私人下载下载器;
public ProgressableStreamContent(流内容,下载下载程序):此(内容,defaultBufferSize,下载程序){}
public ProgressableStreamContent(流内容、int bufferSize、下载下载程序)
{
if(content==null)
{
抛出新的异常(“内容”);
}
如果(缓冲区大小)
{
var buffer=新字节[this.bufferSize];
变量大小=内容。长度;
var=0;
downloader.ChangeState(DownloadState.PendingUpload);
使用(内容)而不使用(true)
{
var length=content.Read(buffer,0,buffer.length);
如果(长度您可以创建一个自定义的HttpContent,如下所示
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;
}
internal class ProgressableStreamContent : HttpContent
{
private const int defaultBufferSize = 4096;
private Stream content;
private int bufferSize;
private bool contentConsumed;
private Download downloader;
public ProgressableStreamContent(Stream content, Download downloader) : this(content, defaultBufferSize, downloader) {}
public ProgressableStreamContent(Stream content, int bufferSize, Download downloader)
{
if(content == null)
{
throw new ArgumentNullException("content");
}
if(bufferSize <= 0)
{
throw new ArgumentOutOfRangeException("bufferSize");
}
this.content = content;
this.bufferSize = bufferSize;
this.downloader = downloader;
}
protected override Task SerializeToStreamAsync(Stream stream, TransportContext context)
{
Contract.Assert(stream != null);
PrepareContent();
return Task.Run(() =>
{
var buffer = new Byte[this.bufferSize];
var size = content.Length;
var uploaded = 0;
downloader.ChangeState(DownloadState.PendingUpload);
using(content) while(true)
{
var length = content.Read(buffer, 0, buffer.Length);
if(length <= 0) break;
downloader.Uploaded = uploaded += length;
stream.Write(buffer, 0, length);
downloader.ChangeState(DownloadState.Uploading);
}
downloader.ChangeState(DownloadState.PendingResponse);
});
}
protected override bool TryComputeLength(out long length)
{
length = content.Length;
return true;
}
protected override void Dispose(bool disposing)
{
if(disposing)
{
content.Dispose();
}
base.Dispose(disposing);
}
private void PrepareContent()
{
if(contentConsumed)
{
// If the content needs to be written to a target stream a 2nd time, then the stream must support
// seeking (e.g. a FileStream), otherwise the stream can't be copied a second time to a target
// stream (e.g. a NetworkStream).
if(content.CanSeek)
{
content.Position = 0;
}
else
{
throw new InvalidOperationException("SR.net_http_content_stream_already_read");
}
}
contentConsumed = true;
}
}
内部类ProgressableStreamContent:HttpContent
{
private const int defaultBufferSize=4096;
私有流内容;
私有int缓冲区大小;
私人住宅;
私人下载下载器;
public ProgressableStreamContent(流内容,下载下载程序):此(内容,defaultBufferSize,下载程序){}
public ProgressableStreamContent(流内容、int bufferSize、下载下载程序)
{
if(content==null)
{
抛出新的异常(“内容”);
}
如果(缓冲区大小)
{
var buffer=新字节[this.bufferSize];
变量大小=内容。长度;
var=0;
downloader.ChangeState(DownloadState.PendingUpload);
使用(内容)而不使用(true)
{
var length=content.Read(buffer,0,buffer.length);
如果(长度)谢谢回复@Lucas Zhang-MSFT,我会试试这个。谢谢回复@Lucas Zhang-MSFT,我会试试这个。