E. Meyers
E. Meyers

Reputation: 31

Uploading to Azure File Share from a Razor Page

I am trying to find an example of uploading a file to an Azure file share from a razor page. I would like to be able to select a file and then have that file saved to the share. I am using Visual Studio 2017, .Net Core 2.0. The only examples I am finding are for Blob storage. Any help would be much appreciated.

Upvotes: 2

Views: 1007

Answers (2)

Stuart Allen
Stuart Allen

Reputation: 691

Here is a simple method I'm using to upload a single file to an endpoint.

 [HttpPost]
    public async Task<IActionResult> Upload(IFormFile file)
    {
        if (file != null)
        {
            using (var stream = new MemoryStream())
            {
                try
                {
                    // assume a single file POST
                    await file.CopyToAsync(stream);
                    stream.Seek(0, SeekOrigin.Begin);

                    // now send up to Azure
                    var filename = file.FileName;
                    var storageAccount = CloudStorageAccount.Parse(<YOUR CREDS HERE>);
                    var client = storageAccount.CreateCloudFileClient();
                    var shareref = client.GetShareReference("YOUR FILES SHARE");
                    var rootdir = shareref.GetRootDirectoryReference();
                    var fileref = rootdir.GetFileReference(filename);
                    await fileref.DeleteIfExistsAsync();
                    await fileref.UploadFromStreamAsync(stream);

                    return Ok(new { fileuploaded = true });
                }
                catch (Exception ex)
                {
                    return BadRequest(ex);
                }
            }
        }
        else
        {
            return BadRequest(new { error = "there was no uploaded file" });
        }
    }

Upvotes: 0

E. Meyers
E. Meyers

Reputation: 31

[HttpPost]

    public IActionResult Index(Microsoft.AspNetCore.Http.IFormFile files)
    {

        string storageConnectionString = "connectionstring to your azure file share";
        CloudStorageAccount cloudStorageAccount = CloudStorageAccount.Parse(storageConnectionString);
        CloudFileClient cloudFileClient = cloudStorageAccount.CreateCloudFileClient();
        CloudFileShare cloudFileShare = cloudFileClient.GetShareReference("your file share name");
        cloudFileShare.CreateIfNotExistsAsync();
        CloudFileDirectory rootDirectory = cloudFileShare.GetRootDirectoryReference();
        CloudFile file = rootDirectory.GetFileReference(files.FileName);
        TransferManager.Configurations.ParallelOperations = 64;
        // Setup the transfer context and track the upoload progress
        SingleTransferContext context = new SingleTransferContext();


        using (Stream s1 = files.OpenReadStream())
        {
            var task = TransferManager.UploadAsync(s1, file);
            task.Wait();
        }

        return RedirectToPage("/Index");
    }

Upvotes: 1

Related Questions