you can use this simple function to push a download to the browser. of course, you can only push one file at a time, and you can't have the page write any other content. The good thing about this is that you can call this function after getting the filepath
from a querystring, or from a database, or any other data source you wish. private void PushFile(string fname) { FileInfo f = new FileInfo(fname); Response.Clear(); Response.AddHeader("Content-Disposition", "attachment; filename=" + f.Name); Response.AddHeader("Content-Length",
f.Length.ToString()); Response.AddHeader("Content-Transfer-Encoding","binary"); Response.ContentType = "application/octet-stream"; Response.WriteFile(f.FullName); Response.End(); }
It's nice to be able to send any file on the server to the browser, but i'v found out that when you use this method the server load the entire file into memory before sending it to the browser. This is ok for small files, but if 20 people start a download of
a 100mb file at the same tide, the server dies. I want to create a secure, que based download section to a site, where people can download files wich are not shared directly on the server, but lies in a directory otside the webroot. Also i want to control
how many downloads of one file is allowed at the same time - something like www.fileplanet.com. The problem with the file being loaded into memory, is also an issue when using a asp 3.0 component wich has a download method - this is how i found the problem.
So my question is: Is it possible to send a non-shared file to a browser without having the server loading the file into memory first? And if not, has anyone got any idears for a secure que based download page? /Anders Rosten
I'm using this technique in an application I'm developing, but the problem I'm having is that users must click the Open button twice when prompted to accept the download. If they click Save everything works, but Open must be clicked twice. It also seems that
the content-length header isn't being processed corrected in IE because it still says file size unknown during the download. Any ideas about how to solve these issues? Would a custom HTTP handler solve any of these problems or are they client-side IE bugs?
Josh I get the problem that users have to click the open button twice too - I notice however that this only happens when a download is opened in an external window. For me Word is launched within the ie container and this only requires one click powerpoint
launches outside of ie and 2 clicks are required. I don't know why they react differently so if anyone can explain? Ideally i'd like the applications to open outside ie and only require 1 click... The filesize bit works fine for me so here is some code: Response.Buffer
= True Response.Clear() Response.Cache.SetLastModified(Now) Response.Cache.SetExpires(DateTime.Now.AddYears(1)) Response.Cache.SetCacheability(HttpCacheability.Private) 'add the header that specifies the default filename for the Download/SaveAs dialog Response.AddHeader("Content-Disposition",
"attachment;filename=" & dtr("nvcFilename").ToString) 'works for powerpoint but requires 2 clicks 'Response.AddHeader("Content-Disposition", "inline; filename=" & dtr("nvcFilename").ToString) 'doesn't work for powerpoint 'add the header that specifies the
file size, so that the browser can show the download progress Response.AddHeader("Content-Length", dtr("iBytes").ToString) 'specify that the response is a stream that cannot be read by the client so it must be downloaded to disk -doesn't always work 'Response.ContentType
= "application/octet-stream" Response.ContentType = dtr("vcMimetype").ToString 'send the file stream to the client Response.BinaryWrite(CType(dtr("imgFile"), Byte())) 'stop the execution of this page Response.End()
First off, I need to correct my earlier post. I was also setting caching information and playing with inline which had an impact on how ie was acting - what i was getting was 2 clicks the first time for both word and powerpoint but subsequently 1 click for
word which was cached and launching within ie. I have found a way of getting documents to open on one click but I think it is a kludge and I'll leave it up to you to decide if it is worth pursuing, personally i think i'll just make the users suffer 2 clicks,
it may be useful if you have a very limited range of documents to deploy BUT i'd say the safe way is to stick with the 2 click scenario. The code uses inline rather than attachment Response.Clear() Response.Cache.SetLastModified(Now) Response.Cache.SetExpires(DateTime.Now)
Response.Cache.SetCacheability(HttpCacheability.Private) Response.AppendHeader("Content-Disposition", "inline; filename=" & dtr("nvcFilename").ToString) Response.AppendHeader("Content-Length", dtr("iBytes").ToString) Response.AppendHeader("Content-Transfer-Encoding",
"binary") Response.ContentType = "application/octet-stream" Response.BinaryWrite(CType(dtr("imgFile"), Byte())) this is what happens: powerpoint opens on one click in powerpoint word opens in one click in ie gif opens without prompt in ie paint shop pro image
attempts to open in ie and leads to the broken link image so you could if you wish use the above for powerpoint, word and images and the code below for paint shop pro which still requires 2 clicks but at least it opens. Response.Clear() Response.Cache.SetLastModified(Now)
Response.Cache.SetExpires(DateTime.Now) Response.Cache.SetCacheability(HttpCacheability.Private) Response.AppendHeader("Content-Disposition", "attachment; filename=" & dtr("nvcFilename").ToString) 'specify filename for open file dialog 'Response.AppendHeader("Content-Disposition",
"inline; filename=" & dtr("nvcFilename").ToString) 'specify filename for open file dialog Response.AppendHeader("Content-Length", dtr("iBytes").ToString) 'file size, so browser can show download progress Response.AppendHeader("Content-Transfer-Encoding", "binary")
Response.ContentType = dtr("vcMimetype").ToString 'set http mimetype for output stream 'Response.ContentType = "application/octet-stream" Response.BinaryWrite(CType(dtr("imgFile"), Byte())) 'write binary data to output stream Incidentally the 2 click scenario
is not a new problem - i came accross it last year when i was working with ie4 & ie5 I look forward to hearing from somebody with a clean solution, until then I'm parking this.
Hi, I find some question about push file to the browser 1. use frameset (frmA and frmB) when frmB haved finish runing "downloadfile.aspx?id=xxx" frmA 's
link doesnt work .. i must "Reload" the frmB then everything be okay 2. when the download filename is not pure english (other charset) then file dialog filename is not correct i do anything wrong ? plz help me , tks [code]
=========================== Protected Sub ClickDownLoad(ByVal sender As System.Object, ByVal e As System.EventArgs) Dim SNValue As Label Dim vSQL As String SNValue = myDataList.Items(0).FindControl("SN") vSQL = "select * from TB where SN=" & SNValue.Text Dim
myDB As New KM.DB() Dim reader As SqlClient.SqlDataReader reader = myDB.GetReader(vSQL) If reader.Read Then Dim filename As String = reader.Item("file_name") Response.Clear() Response.Cache.SetLastModified(Now) Response.Cache.SetExpires(DateTime.Now) Response.Cache.SetCacheability(HttpCacheability.Private)
Response.AddHeader("Content-Length", reader.Item("file_size")) Response.AppendHeader("Content-Transfer-Encoding", "binary") Response.AddHeader("Content-Disposition", "attachment; filename=" & filename) Response.BinaryWrite(reader.Item("bin_data")) Response.End()
End If End Sub
you don't. it's the clients choice which application will launch when a download is clicked. this will be set by the mime-type associations on the client machine. i.e. if it is a psd then the association will be photoshop, but gif/jpg will invariably be associated
with a browser application you can set the image mime-type via Response.ContentType but if the client doesn't have the association you want then photoshop won't open the image. unless you have some control over the configuration of the target clients then
your only option is to let those clients handle the file as they will. regards
I tried something like : Response.ClearContent(); Response.ClearHeaders(); Response.Expires=-1; Response.Buffer=true; Response.ContentType="application/mspaint"; Response.AddHeader("Content-Disposition", "attachment;filename=abc.jpeg"); Response.AppendHeader("Content-Transfer-Encoding",
"binary"); Response.BinaryWrite( (Byte[])Session["PicDataFullImageView"]); Response.Flush(); Response.End(); but it is not opening in mspaint.. By the way do you know how upload a file without using <input type=file>??.. I know there is a method in System.Net.WebClient
called UploadFile but i am trying to figure out how to upload it.
Hello, downloading files with this code ist working well, however I need a solution how to send the Data to the client when the file to send (in my case an .exe file) is stored as an Image at an SQL-Server Database. I do not want to create an temp file at the
server and then send this file because I'm not able to see when Download is finished, so that I can delete the temp file. Does anybody have an idea how to do this? Thanks
joelmartinez
Member
40 Points
13 Posts
Pushing a download to the browser
Jul 08, 2002 01:37 PM|LINK
http://codecube.net
andersr
Member
5 Points
1 Post
Re: Pushing a download to the browser
Jul 19, 2002 02:12 AM|LINK
JoshC
Member
19 Points
7 Posts
Re: Pushing a download to the browser
Jul 25, 2002 06:05 PM|LINK
Adrian Berry
Member
25 Points
5 Posts
Re: Pushing a download to the browser
Aug 09, 2002 06:37 PM|LINK
Adrian Berry
Member
25 Points
5 Posts
Re: Pushing a download to the browser
Aug 10, 2002 07:01 PM|LINK
nekocat
Member
5 Points
1 Post
Re: Pushing a download to the browser
Aug 13, 2002 09:54 AM|LINK
greatb78
Member
365 Points
73 Posts
Re: Pushing a download to the browser
Feb 07, 2004 10:06 AM|LINK
Adrian Berry
Member
25 Points
5 Posts
Re: Pushing a download to the browser
Feb 07, 2004 06:19 PM|LINK
greatb78
Member
365 Points
73 Posts
Re: Pushing a download to the browser
Feb 08, 2004 05:45 AM|LINK
mws-h
Member
5 Points
1 Post
Re: Pushing a download to the browser
Feb 13, 2004 09:38 AM|LINK