Home > Software design >  WriteAsync Specified method is not supported?
WriteAsync Specified method is not supported?

Time:01-18

I want to save files async with progress percent. I have the following code :

        public async Task<bool> UploadMultipleFile(List<IFormFile> formFiles)
        {
            try
            {
                if (_isInProgress)
                {
                    return false;
                }

                var totalBytes = formFiles.Sum(f => f.Length);

                foreach (var formFile in formFiles)
                {
                    _isInProgress = true;

                    var buffer = new byte[16 * 1024];

                    await using var fileStream = File.Create(GetUploadPath()     Path.GetExtension(formFile.FileName));
                    await using var stream = formFile.OpenReadStream();

                    long totalReadBytes = 0;
                    int readBytes;

                    while ((readBytes = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
                    {
                        await stream.WriteAsync(buffer, 0, readBytes);

                        totalReadBytes  = readBytes;

                        _completedPercentOfUploadProgress = (int)(totalReadBytes / (float)totalBytes * 100.0);
                        await Task.Delay(10); // It is only to make the process slower
                    }
                }

                _isInProgress = false;

                return await Task.FromResult(true);
            }
            catch
            {
                return await Task.FromResult(false);
            }
        }

but in While Block at WriteAsync Method Call I got Specified method Is not supported Error. any idea to fix it?

Update:

Instead Of Following Code : await stream.WriteAsync(buffer, 0, readBytes)

using: await fileStream.WriteAsync(buffer, 0, readBytes) fixed the Problem, because formFile.OpenReadStream() doesn't allow you to write, so we either need to create new memory stream or need to set to create as fileStream(File.Create method)

CodePudding user response:

While Block at WriteAsync Method Call I got Specified method Is not supported Error. any idea to fix it? throw expetion because its just for reading. When I called WriteAsync in fileStream(File.Create method) it works fine.

Why You Are Getting the Error:

If you debug your stream = formFile.OpenReadStream(); it only allow read operation. But you are trying to execute write operation inside while loop. that is, stream.WriteAsync(buffer, 0, readBytes); which certainly, this method doesn't allow. You can have a look here:

enter image description here

Solution:

Well, In that case we need to create new MemoryStream() inside your while loop which will allow you to execute write operation. You could modify your code snippet as following:

             while ((readBytes = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
                {
                    var ms = new MemoryStream();
                    await ms.WriteAsync(buffer, 0, readBytes);
                    
                    totalReadBytes  = readBytes;
                    _completedPercentOfUploadProgress = (int)(totalReadBytes / (float)totalBytes * 100.0);
                    await Task.Delay(10); // It is only to make the process slower
                }

Note: Here we are creating var ms = new MemoryStream(); because it will allow us WriteAsync operation over our `file stream. As you can see below:

enter image description here

  • Related