A prompt box is displayed when the downloaded file is downloaded or displayed in the browser.

Source: Internet
Author: User

A friend asked me how to write a prompt box for download, as shown below:

Prompt box appears

String strfile = "F: \ a.doc"; // The Path depends on the actual situation
If (! System. Io. file. exists (strfile ))
{
Response. Write ("<script language = 'javascript '> alert ('Sorry, the file does not exist! '); </SCRIPT> ");
Return;
}
Response. Clear ();
Response. clearheaders ();
Response. charset = "gb2312 ";
Response. contentencoding = system. Text. encoding. utf8;
Response. contenttype = "application/octet-stream ";
Fileinfo Fi = new fileinfo (strfile );
Response. addheader ("content-disposition", "attachment; filename =" + httputility. urlencode (Fi. Name ));
Response. addheader ("Content-Length", Fi. length. tostring ());
Byte [] tmpbyte = new byte [1024*8];
Filestream FS = Fi. openread ();
Int count;
While (COUNT = FS. Read (tmpbyte, 0, tmpbyte. Length)> 0)
{
Response. binarywrite (tmpbyte );
Response. Flush ();
}
FS. Close ();
Response. End ();

open
string strfile = "F: \ a.doc" directly in the browser; // The Path depends on the actual situation
response. clear ();
response. clearheaders ();
response. charset = "gb2312";
response. contentencoding = system. text. encoding. utf8;
response. contenttype = "application/MSWord";
response. writefile (strfile);

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.