How do I dispose my filestream when implementing a file download in ASP.NET?

You don’t need to dispose the stream. It will be disposed by the FileStreamResult.WriteFile method. Code excerpt from this class: public FileStreamResult(Stream fileStream, string contentType) : base(contentType) { if (fileStream == null) { throw new ArgumentNullException(“fileStream”); } this.FileStream = fileStream; } protected override void WriteFile(HttpResponseBase response) { Stream outputStream = response.OutputStream; using (this.FileStream) { byte[] … Read more

Ruby on Rails 3: Streaming data through Rails to client

Assign to response_body an object that responds to #each: class Streamer def each 10_000_000.times do |i| yield “This is line #{i}\n” end end end self.response_body = Streamer.new If you are using 1.9.x or the Backports gem, you can write this more compactly using Enumerator.new: self.response_body = Enumerator.new do |y| 10_000_000.times do |i| y << “This … Read more

HTML5 – How to stream large .mp4 files?

Ensure that the moov (metadata) is before the mdat (audio/video data). This is also called “fast start” or “web optimized”. For example, Handbrake has a “Web Optimized” checkbox, and ffmpeg and avconv have the output option -movflags faststart. Ensure that your web server is reporting the correct Content-Type (video/mp4). Ensure that your web server is … Read more

Download image from the site in .NET/C#

There is no need to involve any image classes, you can simply call WebClient.DownloadFile: string localFilename = @”c:\localpath\tofile.jpg”; using(WebClient client = new WebClient()) { client.DownloadFile(“http://www.example.com/image.jpg”, localFilename); } Update Since you will want to check whether the file exists and download the file if it does, it’s better to do this within the same request. So … Read more