locked
How to connect Azure File Services to a web application made with Asp.net mvc RRS feed

  • Question

  • User-1404740798 posted

    ASP.NET web application created And we created a file share using one of the storage services, the file service.

    I want to download the files that are on this file share from a website that has been made into a web application.

    Is it possible?

    I'd appreciate it if you could tell me how.

    Monday, May 28, 2018 5:11 AM

All replies

  • User283571144 posted

    Hi slkim,

    According to your description, I suggest you could use WindowsAzure.Storage library to help you download the file from Azure File share.

    You could use CloudFile.DownloadToStreamAsync method to download the file and return the view.

    More details, you could refer to below codes:

    View:

    <a href="/home/GetFileFromFileshare/1">Click to get file</a>
    

    Controller:

            public ActionResult GetFileFromFileshare(string id)
            {
                MemoryStream ms = new MemoryStream();
    
                CloudStorageAccount storageAccount = CloudStorageAccount.Parse("DefaultEndpointsProtocol=https;AccountName=fehanteststorage;AccountKey=os6wlWkQoQYUQUCZGDcSUBXzIl+u5S8IXSGxaYTQuo5Ueh2ppexIvt096j1IyRTJazqqRgrTkdaB6VtCSthE3A==;EndpointSuffix=core.windows.net");
                // Create a CloudFileClient object for credentialed access to Azure Files.
                CloudFileClient fileClient = storageAccount.CreateCloudFileClient();
    
                // Get a reference to the file share we created previously.
                CloudFileShare share = fileClient.GetShareReference("botfunctest");
    
                // Ensure that the share exists.
                if (share.Exists())
                {
                    // Get a reference to the root directory for the share.
                    CloudFileDirectory rootDir = share.GetRootDirectoryReference();
    
                    // Get a reference to the directory we created previously.
                    CloudFileDirectory sampleDir = rootDir.GetDirectoryReference("data");
    
                    // Ensure that the directory exists.
                    if (sampleDir.Exists())
                    {
                        // Get a reference to the file we created previously.
                        CloudFile file = sampleDir.GetFileReference("17.gif");
    
                        // Ensure that the file exists.
                        if (file.Exists())
                        {
                            // Write the contents of the file to the console window.
                            file.DownloadToStreamAsync(ms);
                            Stream blobStream =   file.OpenReadAsync().Result;
                            return File(blobStream, file.Properties.ContentType, file.Name);
                        }
                        else
                        {
                            return Content("File does not exist");
                        }
                    }
                    else
                    {
                        return Content("Dir does not exist");
                    }
                }
                else
                {
                    return Content("File does not exist");
                }
            }

    Result:

    Best Regards,

    Brando

    Tuesday, May 29, 2018 6:52 AM
  • User345724256 posted

    Hello Brando.

    Your post was very useful and I take your code to complete my Azure storage project.

    I emphasize this part:

    public async Task<ActionResult> for the GetFileFromFileshare() method, because 

    file.DownloadToStreamAsync(ms);

    is an Asyn Method and the compiler marks an error.

    Thanks!

    Wednesday, July 3, 2019 2:52 PM