Download to client

justputitdown

Member
Joined
Aug 6, 2006
Messages
7
Programming Experience
Beginner
Hi, im a little stuck and not too sure where to look as i havnt been using .Net for that long. I am using Visual Studio 2003 and i am creating a webforms application. For part of it a user can click on a button in a datagrid and depending on the row i want them to be able to download a specific file... Now i no that sounds simple but i dont want to create dynamic <a></a> links as i want to have more control over the download, ie monitor download progress etc... I have read about the Webclient object and methods and although it sounds handy i dont think it gives me that much control. Could someone maybe point me in the direction of a control or code that could possibly help? Some people have mentioned using streams but again im not quite sure where to start.

Thank you for your time.
 
From 4.13 How can I force a Save As dialog box from an ASP.NET Web page.
VB.NET:
Response.Clear() 
Response.AppendHeader("content-disposition", "attachment; filename=document1.doc") 
Response.ContentType = "text/plain" 
Response.WriteFile(Server.MapPath("document1.doc")) 
Response.Flush() 
Response.End()
Instead of the Response.WriteFile line that does the full file stream, you can do it 'manually' like this:
VB.NET:
Dim fs As New IO.FileStream(Server.MapPath("document1.doc"), IO.FileMode.Open, IO.FileAccess.Read)
Dim read, total As Integer, buffer(1024) As Byte
Do Until total = fs.Length
    read = fs.Read(buffer, 0, buffer.Length)
    Response.OutputStream.Write(buffer, 0, read)
    total += read
Loop
fs.Close()
Now you have full control of the client download progress from server-side and can do what you want with that information.

Please update your forum profile, it now says you're on VB2005.
 
Thanks

Thank you for such a quick response! That looks like it is exactly what i am after, i will have a look at it and then give it a go... Think i may use this as a starting point to what will hopefully be a steep learning curve... Thank you very much for all your help.
 
I have a question. What is the major differences if any with the two options given above? I am still learning this myself and if the second option gives more flexability then I am curious as to what I can do with it. I just don't understand how the Response Class works when it comes to things like this.
 
Back
Top