我有用于上传适用于 UWP 的文件的代码。我一直在寻找取消上传的方法。没找到。有办法取消吗?我尝试添加
CancellationToken
但在 DriveAPI
属性中没有地方可以设置它。
try
{
StorageFile file = StorageFile.GetFileFromPathAsync(path).AsTask().Result;
String ID;
var fileMetadata = new Google.Apis.Drive.v3.Data.File()
{
Name = Path.GetFileName(file.Path),
MimeType = "application/octet-stream"
};
using (var stream = file.OpenStreamForReadAsync().Result)
{
var request = this.drive.Files.Create(fileMetadata, stream, "application/octet-stream");
request.ProgressChanged += (IUploadProgress progressInfo) =>
{
if (progressInfo.Status == UploadStatus.Completed)
{
Debug.WriteLine($"done!");
prog.Report(progressInfo);
}
else if (progressInfo.Status == UploadStatus.Failed)
{
Debug.WriteLine($"Failed To Upload Into Google Drive");
prog.Report(progressInfo);
}
else
{
Debug.WriteLine(Convert.ToDouble(progressInfo.BytesSent/(1024*1024)) + " MB has sent");
prog.Report(progressInfo);
}
};
request.ChunkSize = 262144;
request.Upload();
var uploadedFile = request.ResponseBody;
Debug.WriteLine($"File uploaded: {uploadedFile.Name} ({uploadedFile.Id})");
ID = uploadedFile.Id;
return ID;
}
}
我尝试将
if(cancelationtoken)
放入progressInfo.Status == UploadStatus.Uploading
中,但它并没有停止上传。它只会停止反驳“进度”。
有人知道如何处理整个
Task<>
?
这就是我尝试做的:
request.ProgressChanged += (IUploadProgress progressInfo) =>
{
if (progressInfo.Status == UploadStatus.Completed)
{
Debug.WriteLine($"done!");
prog.Report(progressInfo);
}
else if (progressInfo.Status == UploadStatus.Failed)
{
Debug.WriteLine($"Failed To Upload Into Google Drive");
prog.Report(progressInfo);
}
else if (progressInfo.Status == UploadStatus.Uploading)
{
//(if(canceltoken.iscancel)...){}
Debug.WriteLine(Convert.ToDouble(progressInfo.BytesSent/(1024*1024)) + " MB has sent");
prog.Report(progressInfo);
}
};
您希望能够取消文件上传,通过设置您的上传方法
async
可以使这变得更容易。然后您就可以正确使用 await 运算符,并可以按以下格式将取消令牌放入上传调用中:
await request.UploadAsync(cancellationToken)
我注意到代码中的几个地方您将
async
方法降级为阻止调用,例如
StorageFile.GetFileFromPathAsync(path).AsTask().Result
或
var stream = file.OpenStreamForReadAsync().Result
这在某种程度上违背了这些方法异步的目的,并且您可能会遇到一些死锁或其他线程问题。
所以我想说,像这样重新构建你的方法可能非常有利:
async Task<string> Upload(string path, CancellationToken cancellationToken, IProgress<IUploadProgress> prog)
{
StorageFile file = await StorageFile.GetFileFromPathAsync(path);
string ID;
var fileMetadata = new Metadata()
{
Name = Path.GetFileName(file.Path),
MimeType = "application/octet-stream"
};
using (var stream = await file.OpenStreamForReadAsync())
{
var request = this.drive.Files.Create(fileMetadata, stream, "application/octet-stream");
request.ProgressChanged += (IUploadProgress progressInfo) =>
{
if (progressInfo.Status == UploadStatus.Completed)
{
Debug.WriteLine($"done!");
prog.Report(progressInfo);
}
else if (progressInfo.Status == UploadStatus.Failed)
{
Debug.WriteLine($"Failed To Upload Into Google Drive");
prog.Report(progressInfo);
}
else
{
Debug.WriteLine(Convert.ToDouble(progressInfo.BytesSent / (1024 * 1024)) + " MB has sent");
prog.Report(progressInfo);
}
};
request.ChunkSize = 262144;
await request.UploadAsync(cancellationToken);
var uploadedFile = request.ResponseBody;
Debug.WriteLine($"File uploaded: {uploadedFile.Name} ({uploadedFile.Id})");
ID = uploadedFile.Id;
return ID;
}
}