使用这些方法的正确方法是什么?
据我所知,UploadFromByteArrayAsync 方法将返回一个任务,该任务执行异步操作以将字节数组的内容上传到文件。
它将数据异步传输到 can 文件。
更多细节,您可以参考以下代码:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
"connectionstring");
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
CloudFileShare share = fileClient.GetShareReference("filesharename");
CloudFileDirectory rootDir = share.GetRootDirectoryReference();
int size = 5 * 1024 * 1024;
byte[] buffer = new byte[size];
Random rand = new Random();
rand.NextBytes(buffer);
CloudFile file = rootDir.GetFileReference("Log3.txt");
file.UploadFromByteArrayAsync(buffer, 0, buffer.Length);
BeginUploadFromByteArray 方法开始一个异步操作,将字节数组的内容上传到文件。
通过使用此方法,您的程序会将数据作为 UploadFromByteArrayAsync 异步传输到 can 文件。
如果要使用方法,则需要创建一个回调方法以在异步操作完成时接收通知。
更多细节,您可以参考以下代码:
static void Main(string[] args)
{
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
"connectionstring");
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
CloudFileShare share = fileClient.GetShareReference("fileshare");
CloudFileDirectory rootDir = share.GetRootDirectoryReference();
int size = 5 * 1024 * 1024;
byte[] buffer = new byte[size];
Random rand = new Random();
rand.NextBytes(buffer);
CloudFile file = rootDir.GetFileReference("Log3.txt");
//This string will pass to the callback function
string result = "aa";
//Begins an asynchronous operation to upload the contents of a byte array to a file. If the file already exists on the service, it will be overwritten.
var res = file.BeginUploadFromByteArray(buffer, 0, buffer.Length, ProcessInformation, result);
}
static void ProcessInformation(IAsyncResult result)
{
//The callback delegate that will receive notification when the asynchronous operation completes.
string Name = (string)result.AsyncState;
//this Name is aa
Console.WriteLine(Name);
Console.WriteLine("Complete");
}
EndUploadFromByteArray 方法将等待 BeginUploadFromByteArray 方法完全执行。
关于如何使用它,您可以参考以下代码:
static void Main(string[] args)
{
// TableTest();
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(
"connectionstring");
CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
CloudFileShare share = fileClient.GetShareReference("fileshare");
CloudFileDirectory rootDir = share.GetRootDirectoryReference();
int size = 5 * 1024 * 1024;
byte[] buffer = new byte[size];
Random rand = new Random();
rand.NextBytes(buffer);
CloudFile file = rootDir.GetFileReference("Log3.txt");
file.UploadFromByteArrayAsync(buffer, 0, buffer.Length);
string result = "aa";
//Begins an asynchronous operation to upload the contents of a byte array to a file. If the file already exists on the service, it will be overwritten.
var res = file.BeginUploadFromByteArray(buffer, 0, buffer.Length, ProcessInformation, result);
//Ends an asynchronous operation to upload the contents of a byte array to a file.
//wait for the BeginUploadFromByteArray method execute completely then continue run the codes
file.EndUploadFromByteArray(res);
Console.ReadLine();
}
static void ProcessInformation(IAsyncResult result)
{
//The callback delegate that will receive notification when the asynchronous operation completes.
string Name = (string)result.AsyncState;
//this Name is aa
Console.WriteLine(Name);
Console.WriteLine("Complete");
}