Perry
Perry

Reputation: 1337

Why am I not getting any data with this FileStream

I am a Jr. Programmer trying to get mp4 videos from an API and save them to a folder on the network. I am using the following code;

public static void saveVideos(HttpContent content, String filename, bool overwrite)
    {
        string pathName = Path.GetFullPath(filename);
        using (FileStream fs = new FileStream(pathName, FileMode.Create, FileAccess.Write, FileShare.None))
        {
            if (fs.CanWrite)
            {
                byte[] buffer = Encoding.UTF8.GetBytes(content.ToString());
                fs.Write(buffer, 0, buffer.Length);
                fs.Flush();
                fs.Close();

            }
        }
    }

The code will compile without errors but all videos are written to the folder with a size of 1KB. I can't seem to figure out why I am not getting all of the file.

When I inspect the value of the content I see I am getting data that looks like this:

Headers = {Content-Length: 240634544
Content-Disposition: attachment; filename=Orders.dat
Content-Type: application/x-www-form-urlencoded; charset=utf-8

Can anyone point out my error here?

Thanks

Upvotes: 0

Views: 524

Answers (2)

Perry
Perry

Reputation: 1337

If anyone else is having an issue or learning how to save a file using a FileStream hear is the code I used to solve the problem.

public static void saveVideos(HttpContent content, String filename, bool overwrite)
    {
        string pathName = Path.GetFullPath(filename);
        using (Stream fs = new FileStream(pathName, FileMode.Create, FileAccess.Write, FileShare.None, 4096, FileOptions.None))
        {
               byte[] buffer = content.ReadAsByteArrayAsync().Result;
               fs.Write(buffer, 0, buffer.Length);
        }
    }

Upvotes: 0

David
David

Reputation: 218867

This doesn't do what you think it does:

content.ToString()

Unless otherwise overridden, the default implementation of .ToString() simply prints the name of the class. So all you're saving is a bunch of files with class names in them.

What you're probably looking for is the .ReadAsStringAsync() method instead. Or, since you're converting it to bytes anyway, .ReadAsByteArrayAsync() would also work. Perhaps something like this:

byte[] buffer = await content.ReadAsByteArrayAsync();

Of course, since this uses await then your method would have to be async:

public static async Task saveVideos(HttpContent content, String filename, bool overwrite)

If you can't make use of async and await (if you're on an older .NET version), then there are other ways to handle it as well.


Edit: Based on the comments and some of the context of the question, you may be dealing with large files here. In that case, using streams would perform a lot better. I don't have any sample code at the moment, but essentially what you'd want to do is move data directly from one stream to another rather than storing it in an in-memory variable.

Upvotes: 4

Related Questions