我无法从Microsoft中找到有关正确使用 CloudFile 类从 MVC 应用程序中将文件上传到 Azure 文件存储的示例。 Microsoft的文档显示了 CloudFile。UploadFromByteArrayAsync 方法。由于我将文件内容作为字节[],因此UploadFromByteArrayAsync似乎是将文件内容保存到Azure共享上的文件位置的正确方法。
但是 CloudFile 类也有开始上传和结束上传方法。在什么情况下我需要使用这些方法?
方法的正确方法是什么?
据我所知,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);
方法开始异步操作,将字节数组的内容上载到文件。
通过使用此方法,程序会将数据异步传输到 can 文件作为 UploadFromByteArrayAsync。
如果要使用方法,则需要创建一个回调方法,以便在异步操作完成时接收通知。
更多详细信息,您可以参考以下代码:
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");
}
试试这个:
CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
// Create the blob client.
CloudBlobClient blobClient = storageAccount.CreateCloudBlobClient();
// Retrieve reference to a previously created container.
CloudBlobContainer container = blobClient.GetContainerReference(containerName.ToString().ToLower());
await container.CreateIfNotExistsAsync();
// Retrieve reference to blob
CloudBlockBlob blockBlob = container.GetBlockBlobReference(blobRef);
// Upload the file
await blockBlob.UploadFromByteArrayAsync(myByteArray, 0, myByteArray.Length);