我具有上传文件功能。我以块为单位上传文件,它尝试上传块,登录失败后尝试再次登录,然后重试上传。在某些文件上它可以正常工作,而在某些文件上则不能(关于哪些文件失败和哪些文件上载没有一致性-同一文件一次可以工作而另一种则不能)。
我在上传文件功能时遇到的错误:
System.ObjectDisposedException:无法访问已处置的对象。 对象名称:“ System.Net.Http.MultipartFormDataContent”。在 System.Net.Http.HttpContent.CheckDisposed()在 System.Net.Http.HttpContent.CopyToAsync(Stream stream, TransportContext上下文)位于 System.Net.Http.HttpClientHandler.GetRequestStreamCallback(IAsyncResult ar) ---从之前引发异常的位置开始的堆栈跟踪- System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务 任务) System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务 任务),位于System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() 在
功能:
private bool uploadFile(string path, ref string fileName)
{
var chunkSize = 1024 * 1024;
var identifier = Guid.NewGuid();
using (var fileStream = File.OpenRead(path))
using (var client = new HttpClient())
{
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
var count = 1;
var totalChunkCount = Math.Ceiling((double)fileStream.Length / chunkSize);
HttpResponseMessage response = null;
while (fileStream.Position < fileStream.Length)
{
var readCount = fileStream.Length - fileStream.Position > chunkSize
? chunkSize
: (int)(fileStream.Length - fileStream.Position);
var buffer = new byte[readCount];
fileStream.Read(buffer, 0, readCount);
using (var chunkedStream = new MemoryStream(buffer))
{
using (var content = new MultipartFormDataContent())
{
content.Add(new StreamContent(chunkedStream),
Path.GetFileNameWithoutExtension(path), Path.GetFileName(path));
content.Add(new StringContent(count.ToString()), "ChunkNumber");
content.Add(new StringContent(totalChunkCount.ToString()), "TotalChunks");
content.Add(new StringContent(identifier), "Identifier");
content.Add(new StringContent(Path.GetFileName(path)), "Filename");
response = client.PostAsync(m_uploadURL, content).GetAwaiter().GetResult();
if (!response.IsSuccessStatusCode && response.StatusCode == HttpStatusCode.Unauthorized)
{
login();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", accessToken);
response = client.PostAsync(m_uploadURL, content).GetAwaiter().GetResult();
}
}
}
ShowPercentProgress("Upload of " + path, count, Convert.ToInt32(totalChunkCount));
count++;
}
if (response != null)
{
var contents = response.Content.ReadAsStringAsync().GetAwaiter().GetResult();
var json = JObject.Parse(contents);
var uploadResponse = json.ToObject<UploadResponse>();
if (!uploadResponse.status.isSuccess)
{
Logger.Log("Error: " + uploadResponse.status.statusDescription);
return false;
}
if (uploadResponse.data.allChunksUploaded)
{
serverFileName = uploadResponse.data.fileName;
return true;
}
}
}
return false;
}
在网上找不到答案
答案 0 :(得分:0)
各种内容都用大括号括起来。而且您正在使用postbasync。因此,它异步调用,当它从var scope移出时,内容对象会自动处置。 尝试
var content = new MultipartFormDataContent():
content.Add(new StreamContent(chunkedStream),
Path.GetFileNameWithoutExtension(path), Path.GetFileName(path));
content.Add(new StringContent(count.ToString()), "ChunkNumber");
content.Add(new StringContent(totalChunkCount.ToString()), "TotalChunks");
content.Add(new StringContent(identifier), "Identifier");
content.Add(new StringContent(Path.GetFileName(path)), "Filename");
使用删除。