Search code examples
asp.netasp.net-coreazure-storage

POST method to upload file to Azure storage - what to return


I am creating an app where

  1. user can upload the text file and then
  2. find most used word and change that word in text and
  3. show the changed text to the user.

if it is possible, I would like to

  1. get the file’s text content before uploading when Post method is being called and save that content

so I add the “DownloadTextAsync()” method inside of the POST method, but it seems like I am calling this method to the wrong subject?

 [HttpPost("UploadText")]
        public async Task<IActionResult> Post(List<IFormFile> files)
        {

                    string connectionString = Environment.GetEnvironmentVariable("mykeystringhere");

                    // Create a BlobServiceClient object which will be used to create a container client
                    BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);

                    //Create a unique name for the container
                    string containerName = "textdata" + Guid.NewGuid().ToString();

                    // Create the container and return a container client object
                    BlobContainerClient containerClient = await blobServiceClient.CreateBlobContainerAsync(containerName);

                    // Create a local file in the ./data/ directory for uploading and downloading
                    string localPath = "./data/";
                    string fileName = "textfiledata" + Guid.NewGuid().ToString() + ".txt";
                    string localFilePath = Path.Combine(localPath, fileName);

                    // Get a reference to a blob
                    BlobClient blobClient = containerClient.GetBlobClient(fileName);

                    // Open the file and upload its data
                    using FileStream uploadFileStream = System.IO.File.OpenRead(localFilePath);
                    await blobClient.UploadAsync(uploadFileStream, true);
                    uploadFileStream.Close();

                    string downloadFilePath = localFilePath.Replace(".txt", "DOWNLOAD.txt");


                    // Get the blob file as text
                    string contents = blobClient.DownloadTextAsync().Result;

                    //return the string 
                    return contents;     


            //if (uploadSuccess)
            //    return View("UploadSuccess");
            //else
            //    return View("UploadError");
        }

The issues I am having are

  1. I understood that ‘blobClient’ is the reference to the blob, where I can get the file’s data but this must be wrong?

  2. Also it seems like I cannot use “CloudBlobContainer” nor the “CloudBlockBlob blob”. Is it because inside of the POST method, the blob has been just initialized and does not exist when these twos are executed?

  3. Also when I test the POST method, the console throws “Refused to load the font '' because it violates the following Content Security Policy directive: "default-src 'none'". Note that 'font-src' was not explicitly set, so 'default-src' is used as a fallback.” which I googled but have no idea what it means? I have tried different ways but keep getting CANNOT POST/“ But could not really find the solid anwers. Could this be related to my POST method?


Solution

  • I understood that ‘blobClient’ is the reference to the blob, where I can get the file’s data but this must be wrong?

    That's correct in a sense that you can use blobClient to perform operations on blob like upload/download etc. I am not sure why you say but this must be wrong.

    Also it seems like I cannot use “CloudBlobContainer” nor the “CloudBlockBlob blob”. Is it because inside of the POST method, the blob has been just initialized and does not exist when these twos are executed?

    No, this is happening because you're using a newer version of SDK (version 12.x.x) and CloudBlobContainer and CloudBlockBlob are available in the older version of the SDK.

    Also when I test the POST method, the console throws “Refused to load the font '' because it violates the following Content Security Policy directive: "default-src 'none'". Note that 'font-src' was not explicitly set, so 'default-src' is used as a fallback.” which I googled but have no idea what it means? I have tried different ways but keep getting CANNOT POST/“ But could not really find the solid anwers. Could this be related to my POST method?

    Not sure why this is happening. You may want to ask a separate question for this and when you do, please include the HTML portion of your code as well.