asp.net download Web page program showing download prompts

Source: Internet
Author: User

Web site production is often to develop the function of downloading files, the following three ways to download the file:


1, ASP implementation of the download code
<%


filename = request.querystring ("filename")


If filename = "" Then


Response.Write "Please enter filename parameter, specify download filename"


Else


Response.ContentType = "Application/octet-stream"


Response.AddHeader "content-disposition", "attachment;" filename = "& filename


Set FileStream = Server.CreateObject ("ADODB.stream")


Filestream.mode = 3


Filestream.type = 1


Filestream.open


Filestream.loadfromfile (Server.MapPath (filename))





Response.BinaryWrite (FileStream.Read)





filestream.close ()


Set FileStream = Nothing


End If


%> The above code into an ASP-type file, which is similar when used: Download.asp?filename=a.gif.


2, using WebClient
Add the following code in the Download button event


System.Net.WebClient WC = new System.Net.WebClient ();


WC. DownloadFile ("Http://localhost/a.gif", "c:a.gif");


The above code will put the server-side a.gif files in the absence of any hint to download the client's C disk, there is no hint or more frightening, but sometimes it is really necessary to do so. The code can also be run on a desktop program.



asp.net download Web page program showing download prompts
//Open the file to download


System.IO.FileStream r = new System.IO.FileStream (FileName, System.IO.FileMode.Open);


//Set basic information


Response.Buffer = false;


Response.AddHeader ("Connection", "keep-alive");


Response.ContentType = "Application/octet-stream";


Response.AddHeader ("Content-disposition", "attachment;filename=" + System.IO.Path.GetFileName (filename));


Response.AddHeader ("Content-length", r.length.tostring ());


         


while (true)


{


//Open buffer space


byte[] buffer = new byte[1024];


//Read the file data


int leng = r.read (buffer, 0, 1024);


if (leng = = 0)//To end of file, ending


break;


if (leng = = 1024)//read out file data length equals buffer length, write buffer data directly to


Response.BinaryWrite (buffer);


Else


            {


//read out file data is smaller than the buffer, redefine the buffer size, only to read the last block of data in the file


byte[] b = new Byte[leng];


for (int i = 0; i < Leng; i++)


B[i] = buffer[i];


Response.BinaryWrite (b);


            }


        }


r.close ()//close Download file


Response.End ()//end File Download


This method has the download prompt box, the server side may know when the download completes.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.