Download Filestream Driver



So I built a service that collects information, say, log information, and sends it down to a client. This information is normally streamed right to a web browser via SignalR, but sometimes there is a lot of it, and scrolling through it on the web view can be quite a pain. It would be nice to be able to download the data as a tab-delimited file or something like that, so I can just open it with my favorite text editor and use the search functionality. The service doesn’t persist the data forever, either, so if I want to save the information to look at later, I have to save it somehow.

I have exactly the same issue. I deleted the Drive FS folder and reinstalled Filestream. That worked for a few days and then folders and files disappeared again. I have plenty of disk space so that is not the cause. It seems, but I am not sure, that older or unused files are exempted from sync. I could not find any setting that would cause this. Altitude check for the SQL Server FILESTREAM filter driver e.g. Evaluate all the filter drivers loaded for the storage stack associated with a volume where the FILESTREAM feature stores files and make sure that rsfx driver is located at the bottom of the stack.

Since I’m using an Angular SPA, where all server access is via WebAPI. The information doesn’t exist on disk, its all in memory, so you can’t necessarily just open a FileStream and return that.

Download Filestream DriverDownload Filestream Driver

MemoryStream

To make a stream from stuff you have in memory, you use a MemoryStream. Basically, its just an array of bytes. You can write bytes directly to it with the Write method, or you can use a StreamWriter to make it easier to write basic types:

So you can easily put your data in to a stream, but then you need to return it in the right way from the WebAPI.

Download Filestream Drivers

Driver

FileStreamResult

To return a file stream, you can use a FileStreamResult. This lets you specify the stream, the MIME type, and some other options (like the file name).
The Controller has a shortcut for this – you can just return File:

There’s a couple of “gotchas,” which is why I’m writing this blog post. First, note the lack of a using block around the MemoryStream. This is because the using block will dispose/close the stream, which might be premature (before the file is finished being downloaded). ASP.NET will handle getting rid of this stream for you once the transfer is complete (part of the FileStreamResult), so you don’t need to worry about cleaning it up.

Download Filestream Driver Download

Second, note the Seek method being invoked on the MemoryStream to “rewind” it to the beginning. When you’re writing to a stream, you’re advancing the stream. So, when you’re done writing, you need to effectively rewind the stream to make sure the FileStreamResult is at the beginning to begin the download.