首页 > 解决方案 > 无法访问已处置的对象。对象名称:FileBufferingReadStream

问题描述

我正在尝试以天蓝色上传文件。

所以基本上我试图将文件转换成流,这样我就可以在服务器上创建一个文件并写入它的数据。

   public async Task UploadFileOnAzure( string path, string name, IFormFile file)
    {
        //create directory
        await _dlsService.CreateDir(path, name);
        //create file
        var f = file.FileName;
        var ext = Path.GetExtension(f);
        string filepath = $"{path}/{name.ToString()}/{f}";
        try
        {
            using (var ms = new MemoryStream())
            {
                using (var fileStram = file.OpenReadStream())
                {//sometimes it breakes right before this line, other times it doesn't write the file(excel)
                    fileStram.CopyTo(ms);
                    using (Stream str = await _dlsService.CreateFile(filepath)) //file is created as empty ofcourse
                    {
                        ms.CopyTo(str);
                        str.Close();
                        //this doesnt write on the file
                    }
                }
            }
        }
        catch(Exception ex)
        {
            Console.WriteLine(ex.Message.ToString());
        }


    }

无法访问已处置的对象。对象名称:FileBufferingReadStream。我经常收到这个错误。似乎无法理解为什么。我只想写在创建的文件上。请帮助:)

标签: c#.netasp.net-mvcfile-uploadstream

解决方案


推荐阅读