473,513 Members | 2,693 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

streaming random filetypes from .aspx

streaming random filetypes from .aspx

how to stream a file from inside a .aspx? e.g. so one could go
href="./foo.aspx?file=bar.mp3" or href="./foo.aspx?foo.dat" etc. preferabley
i would like all file types to open with "save as" when href clicked
Nov 17 '05 #1
1 1422
Daniel wrote:
streaming random filetypes from .aspx

how to stream a file from inside a .aspx? e.g. so one could go
href="./foo.aspx?file=bar.mp3" or href="./foo.aspx?foo.dat" etc.
preferabley i would like all file types to open with "save as" when
href clicked


the aspx page is clean:
<%@ Page language="c#" Codebehind="StreamAFile.aspx.cs"
AutoEventWireup="false" Inherits="YourNamespace.StreamAFile" %>

the code behind:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;

namespace YourNamespace
{
/// <summary>
/// Generic filestreamer for ASP.NET. Receives the file with the path
relative to the site root folder.
/// </summary>
public class StreamAFile: System.Web.UI.Page
{
private void Page_Load(object sender, System.EventArgs e)
{
string pathFilename = (string)Request.QueryString["Filename"];

// replace "\" with "/"
pathFilename.Replace(@"\", "/");

string filename = pathFilename;
int indexLastSlash = pathFilename.LastIndexOf("/");

if(indexLastSlash>=0)
{
filename = pathFilename.Substring(indexLastSlash);
}

// put the download folder in front of it.
// chop off '..' here if you want to. You can also
// protect it in the webserver.
string completePathFilename =
Path.Combine(Application["sDownloadFilesRootFolder"].ToString(),
filename);

// test if file exists.
if(!File.Exists(completePathFilename))
{
// no. redirect
Response.Redirect("/default.aspx");
}

FileStream stream = new FileStream(completePathFilename,
FileMode.Open, FileAccess.Read);
byte[] fileContents = new byte[stream.Length];

stream.Read(fileContents, 0, (int)stream.Length);
stream.Close();

// create header
Response.ClearHeaders();
Response.AddHeader("Content-Type", "application/unknown");
Response.AddHeader("Content-Disposition", "attachment; filename=" +
filename);
Response.AddHeader("Content-Transfer-Encoding", "Binary");
Response.BinaryWrite(fileContents);
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
}
}

FB
--
------------------------------------------------------------------------
Get LLBLGen Pro, productive O/R mapping for .NET: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
Nov 17 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1823
by: Jakob Bengtsson | last post by:
Hi, Does anyone know how to get the icons for all filetypes installed on the system? I can extract the associated icon from a file, but I don't know how to: 1) Get a list of filetypes; 2) Get...
1
7800
by: Justin Straube | last post by:
Hopefully someone can catch what im missing here. Ive googled this and I think Ive got the filetypes arg written properly, but I get a traceback when calling this function. Heres the code...
1
1807
by: Daniel | last post by:
streaming random filetypes from .aspx how to stream a file from inside a .aspx? e.g. so one could go href="./foo.aspx?file=bar.mp3" or href="./foo.aspx?foo.dat" etc. preferabley i would like...
5
1440
by: Eric Sabine | last post by:
I have a web site where the user is able to download a file. I can't use an anchor tag because I don't want the user to see where the filename is coming from. Below is the code in which the user...
3
2323
by: A.M-SG | last post by:
Hi, I have a ASP.NET aspx file that needs to pass large images from a network storage to client browser. The requirement is that users cannot have access to the network share. The aspx file...
6
2703
by: | last post by:
Hi all, is there a better way to stream binary data stored in a table in sql 2005 to a browser in .net 2.0? Or is the code same as in .net 1.1? We noticed that in certain heavy load scenarios,...
1
5383
by: DB | last post by:
Hi All, I’m trying to stream a PDF with .NET 2.0 in a c# web app. However, it does not actually show the PDF (using adobe acrobat). The Situation: Click on a Button, Opens a new Window,...
3
3996
by: Brad | last post by:
I have an aspx page that is sending pdf files to client browsers: it uses a filestream to read the pdf file and response.binarywrite to send content to the browser. This has worked great for years...
2
1714
by: Bubbs | last post by:
hi, Lemme give u a little background on my problem. Ive created a client and server applications that receive and stream live time values. The client software is a clock that receives the time and...
0
7257
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7535
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
7098
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7521
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5682
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5084
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4745
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3232
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3221
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.