2

I'm trying to upload files in azure.

So basically I'm trying to convert the file in a stream so I can create a file on server and write its data.

   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());
        }


    }

Cannot access a disposed object. Object name: FileBufferingReadStream. I get this error quite often. Can't seem to understand why. I would like to just write on the created file. Pls help :)

Pavel
  • 153
  • 1
  • 4
  • 14
  • A using statement will dispose and object (flush, close as well )outside the clause. You will get an error if you try to do the close in code. – jdweng Oct 08 '19 at 16:02
  • Exactly where are you receiving that error? – Trevor Oct 08 '19 at 16:02
  • @Çöđěxěŕ sometimes i get the error on this line `var fileStram = file.OpenReadStream()` . Other times I don't get an error but nothing is written to the file. Is this a correct way of uploading a file? Or writing one? – Pavel Oct 08 '19 at 16:07
  • @jdweng I already know that, but I'm inside the using statement. So I guess there was not suppossed to be any problem on this case... right?? Can you suggest me a way of how to the file in a stream and than use `copyTo` method to write on the created file? Or any way of uploading a file in azure. I would really appreciate it. – Pavel Oct 08 '19 at 16:09
  • Has the file object already been disposed by the time you get here - file.OpenReadStream()? i.e. where you are calling this function, is there a using block there - I'm going only by the error message and its location. – Rakesh Oct 08 '19 at 16:12
  • @Rakesh No, the has not been disposed. As I said this sometimes happens, and sometimes not. Anyway, why in the cases that doen't raise an exception, it doesn't write to `str` object? Can you give me an example? – Pavel Oct 08 '19 at 16:21
  • Does it EVER write any data - or is there an intermittent issue. Temporarily remove the using blocks to see if that is the source or of if this is coming in from elsewhere. – Rakesh Oct 08 '19 at 16:23
  • No it never writes the data @Rakesh. That's the problem. Even in the cases that it doen;t raise the error. Should I use memory stream? Should I just use stream? Any idea? or approach? – Pavel Oct 08 '19 at 16:28
  • 2
    Remove the using statements and try. We can go from there. – Rakesh Oct 08 '19 at 16:30
  • I tried that and it says `Cannot access a closed Stream.` @Rakesh – Pavel Oct 08 '19 at 16:36
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/200567/discussion-between-rakesh-and-pavel). – Rakesh Oct 08 '19 at 16:37
  • 1
    Look [here](https://stackoverflow.com/questions/3914445/how-to-write-contents-of-one-file-to-another-file) for a similar question on copying file data. – Rakesh Oct 08 '19 at 16:51
  • I found the problem guys. There was a call that I wouldn't await that would couse me this much trouble. Thanks you. – Pavel Oct 08 '19 at 17:05

0 Answers0