Ejemplo n.º 1
0
        static void FileTest()
        {
            CloudStorageAccount storageAccount = ValidateConnection();

            if (storageAccount == null)
            {
                return;
            }
            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
            CloudFileShare  share      = fileClient.GetShareReference("myfiles");

            share.CreateIfNotExists();
            Console.WriteLine("share Created");
            CloudFileDirectory root = share.GetRootDirectoryReference();
            CloudFileDirectory dir  = root.GetDirectoryReference("mydirectory");//create directory

            dir.CreateIfNotExists();
            string filepath = @"d:\test.txt";
            //upload file
            CloudFile file = dir.GetFileReference("t1.txt");

            file.UploadFromFile(filepath);
            Console.WriteLine("File Uploaded");
            //download file
            file = dir.GetFileReference("t1.txt");
            file.DownloadToFile("d:\\t3.txt", FileMode.Append);
            Console.WriteLine("File downloaded");
        }
Ejemplo n.º 2
0
        void LoadXamlPackage4(string _fileName)
        {
            if (CheckForInternetConnection() == true)
            {
                // download done.xaml file from Azure to C:\\Test\\done.xaml
                CloudFile file = sampleDir.GetFileReference("done.xaml");
                file.DownloadToFile(_fileName, System.IO.FileMode.OpenOrCreate);

                // load done.xaml file from C:\\Test\\done.xaml to app
                TextRange  range;
                FileStream fStream;
                if (File.Exists(_fileName))
                {
                    range   = new TextRange(richTextBox4.Document.ContentStart, richTextBox4.Document.ContentEnd);
                    fStream = new FileStream(_fileName, FileMode.OpenOrCreate);
                    range.Load(fStream, DataFormats.XamlPackage);
                    fStream.Close();
                }
            }
            else             // load locally only
            {
                // load done.xaml file from C:\\Test\\done.xaml to app
                TextRange  range;
                FileStream fStream;
                if (File.Exists(_fileName))
                {
                    range   = new TextRange(richTextBox4.Document.ContentStart, richTextBox4.Document.ContentEnd);
                    fStream = new FileStream(_fileName, FileMode.OpenOrCreate);
                    range.Load(fStream, DataFormats.XamlPackage);
                    fStream.Close();
                }
            }
        }
Ejemplo n.º 3
0
        // File Download
        public static void DownloadFileFromFolder(string folderName, string fileName)
        {
            CloudFileDirectory rootDirectory = BasicAzureFileOperations();
            CloudFileDirectory subDirectory  = rootDirectory.GetDirectoryReference(folderName);
            CloudFile          downloadFile  = subDirectory.GetFileReference(fileName);

            downloadFile.DownloadToFile(fileName, FileMode.Create);
        }
Ejemplo n.º 4
0
        // File Download
        public void DownloadFileFromFolder(string folderName, string fileName)
        {
            CloudFileDirectory rootDirectory = fileShare.GetRootDirectoryReference();
            CloudFileDirectory subDirectory  = rootDirectory.GetDirectoryReference(folderName);
            CloudFile          downloadFile  = subDirectory.GetFileReference(fileName);

            downloadFile.DownloadToFile(fileName, FileMode.Create);
        }
Ejemplo n.º 5
0
        public void Download()
        {
            var sw       = Stopwatch.StartNew();
            var fileName = Path.Combine(LocalDirectory.FullName, CloudFile.Name);

            _logger.WriteLine("Downloading {0} ", CloudFile.Name);
            CloudFile.DownloadToFile(fileName, FileMode.OpenOrCreate);
            sw.Stop();
            _logger.WriteLine("Finish {0} download in {1}", CloudFile.Name, sw.Elapsed);
        }
        public void DownloadFileToLocalPath(string fileShareName, string localPath, FileMode mode, 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);
                    switch (mode)
                    {
                    case FileMode.Append:
                        cloudFile.DownloadToFile(localPath, FileMode.Append);
                        break;

                    case FileMode.Create:
                        cloudFile.DownloadToFile(localPath, FileMode.Create);
                        break;

                    case FileMode.CreateNew:
                        cloudFile.DownloadToFile(localPath, FileMode.CreateNew);
                        break;

                    case FileMode.Open:
                        cloudFile.DownloadToFile(localPath, FileMode.Open);
                        break;

                    case FileMode.OpenOrCreate:
                        cloudFile.DownloadToFile(localPath, FileMode.OpenOrCreate);
                        break;

                    case FileMode.Truncate:
                        cloudFile.DownloadToFile(localPath, FileMode.Truncate);
                        break;

                    default:
                        break;
                    }
                }
                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);
            }
        }
Ejemplo n.º 7
0
        public ActionResult getFile(String FileName)
        {
            AzureFileService fileService = new AzureFileService(this._appSettings);
            CloudFile        cloudFile   = fileService.getFIle("files", FileName);
            string           path        = Path.Combine(_env.WebRootPath, "files");

            cloudFile.DownloadToFile(Path.Combine(path, FileName), FileMode.Create);
            var    stream       = new FileStream(Path.Combine(path, FileName), FileMode.Open);
            var    mimeProvider = new FileExtensionContentTypeProvider();
            string mimeType;

            mimeProvider.TryGetContentType(Path.Combine(path, FileName), out mimeType);
            return(File(stream, mimeType));
        }
        public void FileOperations(string fileSharename, string Directory, string filePath)
        {
            CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
            CloudFileShare  fileShare  = fileClient.GetShareReference(fileSharename);

            fileShare.CreateIfNotExists(null, null);
            CloudFileDirectory rootDirectory = fileShare.GetRootDirectoryReference();
            CloudFileDirectory fileDirectory = rootDirectory.GetDirectoryReference(Directory);

            fileDirectory.CreateIfNotExists();
            CloudFile file = fileDirectory.GetFileReference("testfile");

            //Deleting If File Exists
            file.DeleteIfExistsAsync();
            if (file.Exists() == false)
            {
                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
                file.Create(fs.Length);
                fs.Close();
            }
            file.OpenWrite(null);
            //Upload File Operation
            file.UploadFromFile(filePath, FileMode.Open);
            //Write File Operation
            file.WriteRange(new FileStream(filePath, FileMode.Open), 0);
            Stream azureFile = file.OpenRead();

            //Read File Operation
            azureFile.Position = 0;
            byte[] buffer = new byte[azureFile.Length - 1];
            int    n      = azureFile.Read(buffer, (int)0, 14050);

            for (int i = 0; i < buffer.Length; i++)
            {
                Console.Write(buffer[i].ToString());
            }
            //Download File Operation
            file.DownloadToFile(@"D:\TestFile.pptx", FileMode.Create);
        }
Ejemplo n.º 9
0
        public static string DownloadFile(string folderPath, string fileName)
        {
            try
            {
                string localFile = Path.GetTempFileName();

                CloudFileDirectory directory = GetFileDirectory(folderPath);

                CloudFile file = directory.GetFileReference(fileName);

                file.DownloadToFile(localFile, FileMode.Create);

                return(localFile);
            }
            catch (Exception ex)
            {
                if (_feedback != null)
                {
                    _feedback.OnException(fileClient, ex);
                }
            }

            return("");
        }
Ejemplo n.º 10
0
 private static void DownloadFile(CloudFile file)
 {
     file.DownloadToFile("PathAndFileNameOnYourComputer", FileMode.CreateNew);
 }