-1

我无法从 Microsoft 中找到有关正确使用 CloudFile 类从 MVC 应用程序中将文件上传到 Azure 文件存储的示例。Microsoft 的文档显示了Cloud​File.​Upload​From​Byte​Array​Async方法。由于我将文件内容作为字节 [],UploadFromByteArrayAsync 似乎是将文件内容保存到 Azure 共享文件位置的正确方法。

但是 CloudFile 类也有 beginupload 和 endupload方法。在什么条件下我需要使用这些方法?

4

2 回答 2

3

使用这些方法的正确方法是什么?

据我所知,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");


        }
于 2017-05-17T06:37:15.657 回答
1

尝试这个:

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);
于 2017-05-17T05:26:48.117 回答