0

I am trying to upload a document into Azure Blob but it throws an error as below while reading the file stream.

Error,

Cannot access a disposed object.
Object name: 'FileBufferingReadStream'.

The error comes when the code OpenReadStream() called,

public async Task UploadAsync(IFormFileCollection files, string directoryName)
    {
        var blobContainer = await _azureBlobConnectionFactory.GetBlobContainer();
        CloudBlobDirectory directory = blobContainer.GetDirectoryReference(directoryName);
        for (int i = 0; i < files.Count; i++)
        {
            CloudBlockBlob blockblob = directory.GetBlockBlobReference(files[i].FileName);
            using (var stream = files[i].OpenReadStream())
            {
                await blockblob.UploadFromStreamAsync(stream);

            }
        }
    }

I am calling the UploadAsync method from my actual service classs like,

public async Task<bool> UploadToBlob(DocumentModel model,string directorypath)
    {
        try
        {
            //string directory = directorypath + model.EmailId + "/" + model.Files[0].FileName;
            await _blobService.UploadAsync(model.Files, "Documents/dummy.pdf");

           
            return true;
        }
        catch(Exception e)
        {
            throw e;
        }
    }

Where I did go wrong?

Md Aslam
  • 1,228
  • 8
  • 27
  • 61

1 Answers1

1
  • The return method of Action method can be a possible reason for this issue
  • The error occurs if the return type is not Task<T>.
    You can modify your Action Method as follows:
[HttpPost]
public async Task<int> ActionMethod(IFormFile img)
{
     // same

     return resultValue;
}

References :

  1. When trying to upload file, FileBufferingReadStream.ThrowIfDisposed() error occur
  2. Cannot access a disposed object. Object name: FileBufferingReadStream
  3. IFormFile copy to memorystream ObjectDisposedException