public byte[] DownloadFileContentAsByteArray(string fileShareName, string fileName = "")
        {
            CloudFileClient fileClient = new CloudFileClient(fileURI, creds);
            // Create a CloudFileClient object for credentialed access to Azure Files.


            // Get a reference to the file share we created previously.
            CloudFileShare share = fileClient.GetShareReference(fileShareName);

            // Ensure that the share exists.
            if (share.Exists())
            {
                try
                {
                    // Get a reference to the root directory for the share.
                    CloudFileDirectory rootDir   = share.GetRootDirectoryReference();
                    CloudFile          cloudFile = rootDir.GetFileReference(fileName);
                    byte[]             array     = new byte[cloudFile.Properties.Length];
                    cloudFile.DownloadToByteArray(array, 0);
                    return(array);
                }
                catch (Exception e)
                {
                    throw new StorageAccountException("Error while attempting to get contents", e);
                }
            }
            else
            {
                DirectoryNotFoundException e = new DirectoryNotFoundException(string.Format("The file share '{0}' does not exist.", fileShareName));
                throw new StorageAccountException("Error while attempting to get content", e);
            }
        }
Exemple #2
0
        private object GetData(CloudFile file, DownloadDataType downloadDataType)
        {
            switch (downloadDataType)
            {
            case DownloadDataType.Text:
                return(file.DownloadText());

            case DownloadDataType.ByteArray:
                var document = new byte[file.Properties.Length];

                file.DownloadToByteArray(document, 0);

                return(document);

            default:
                throw new ArgumentOutOfRangeException("downloadDataType");
            }
        }
Exemple #3
0
        /// <summary>
        /// Method to get the File from Azure File Service Using FileGuid and Directory Name
        /// </summary>
        /// <param name="directoryname"></param>
        /// <param name="filename"></param>
        /// <returns></returns>
        private byte[] getFileFromAzureFileService(int ModuleID, string filename, bool operDelFile)
        {
            var docconfigdet = GetDocumentShareDetails(ModuleID);

            string[]            directorydet   = docconfigdet.ChildDirectory.Split('/');
            CloudStorageAccount storageAccount = CloudStorageAccount.Parse(CloudConfigurationManager.GetSetting("StorageConnectionString"));
            CloudFileClient     fileClient     = storageAccount.CreateCloudFileClient();
            CloudFileShare      share          = fileClient.GetShareReference(docconfigdet.RootDirectory.ToString());

            if (share.Exists())
            {
                CloudFileDirectory[] filedirectory = new CloudFileDirectory[directorydet.Length];
                var i = 0;
                CloudFileDirectory rootdir = share.GetRootDirectoryReference();

                foreach (var dir in directorydet)
                {
                    if (i == 0)
                    {
                        filedirectory[i] = rootdir.GetDirectoryReference(dir);
                    }
                    else
                    {
                        filedirectory[i] = filedirectory[i - 1].GetDirectoryReference(dir);
                    }


                    if (!filedirectory[i].Exists())
                    {
                        throw new ArgumentOutOfRangeException("File Share Does not Exists");
                    }
                    i++;
                }

                i--;
                try
                {
                    CloudFile downloadfile = filedirectory[i].GetFileReference(filename);
                    if (operDelFile)
                    {
                        downloadfile.Delete();
                        return(null);
                    }

                    if (downloadfile.Exists())
                    {
                        if (downloadfile.Properties.Length <= 0)
                        {
                            throw new ArgumentOutOfRangeException("File Does not Exists...");
                        }
                        byte[] filebytes = new byte[downloadfile.Properties.Length];
                        downloadfile.DownloadToByteArray(filebytes, 0);
                        return(filebytes);
                    }
                    else
                    {
                        throw new Exception("File Not Found...");
                    }
                }
                catch (Exception ex)
                {
                    throw new Exception(ex.ToString());
                }
            }
            else
            {
                throw new ArgumentOutOfRangeException("File Share Does not Exists");
            }
        }