Feeding File from a Different Server

J

Jonathan Wood

Greetings,

I've created a download site using ASP.NET. The files are actually hosted on
the authors' sites and not on my server.

When a user downloads a file, I want to make a note of it in the database.
So I can't just have a regular HTML link to the file. I've come up with a
couple of solutions to this using <iframe> and javascript. But one approach
I was hoping to get more info on is returning the file directly from a
postback.

The only way I see to accomplish this is, when the link posts back, my code
would actually retrieve the file from the author's site and feed it to the
current request response, along with the appropriate headers, as though the
file came from my server.

Can anyone tell me:

A) Would this even work?

B) I suppose I'd just download a block of bytes at a time, passing them on
to the request response until the entire file has been sent. Is there a
higher level way to do this?

C) Is there a better way to accomplish this using ASP.NET?

Thanks for any suggestions.

Jonathan
 
G

Guest

Greetings,

I've created a download site using ASP.NET. The files are actually hosted on
the authors' sites and not on my server.

When a user downloads a file, I want to make a note of it in the database..
So I can't just have a regular HTML link to the file. I've come up with a
couple of solutions to this using <iframe> and javascript. But one approach
I was hoping to get more info on is returning the file directly from a
postback.

The only way I see to accomplish this is, when the link posts back, my code
would actually retrieve the file from the author's site and feed it to the
current request response, along with the appropriate headers, as though the
file came from my server.

Can anyone tell me:

A) Would this even work?

B) I suppose I'd just download a block of bytes at a time, passing them on
to the request response until the entire file has been sent. Is there a
higher level way to do this?

C) Is there a better way to accomplish this using ASP.NET?

Thanks for any suggestions.

Jonathan

Hi Jonathan,

take a look at the code below. It does all what you need except the
database stuff
Hope this helps

using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.Data.Sql;
using System.Net;
using System.IO;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
//base.OnLoad(e);
string url = string.Empty;// Request.QueryString
["DownloadUrl"];
if (url == null || url.Length == 0)
{
url = "http://img444.imageshack.us/img444/6228/
initialgridsq7.jpg";
}
//Initialize the input stream
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
int bufferSize = 1;
//Initialize the output stream
Response.Clear();
Response.AppendHeader("Content-Disposition:", "attachment;
filename=download.jpg");
Response.AppendHeader("Content-Length",
resp.ContentLength.ToString());
Response.ContentType = "application/download";
//Populate the output stream
byte[] ByteBuffer = new byte[bufferSize + 1];
MemoryStream ms = new MemoryStream(ByteBuffer, true);
Stream rs = req.GetResponse().GetResponseStream();
byte[] bytes = new byte[bufferSize + 1];
while (rs.Read(ByteBuffer, 0, ByteBuffer.Length) > 0)
{
Response.BinaryWrite(ms.ToArray());
Response.Flush();
}
//Cleanup
Response.End();
ms.Close();
ms.Dispose();
rs.Dispose();
ByteBuffer = null;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,813
Messages
2,569,696
Members
45,483
Latest member
TedDvb6626

Latest Threads

Top