ASP. NET attachment pop-up window download

Source: Internet
Author: User

You cannot publish any text on the homepage. It hurts a little bit. Aha. This time, I will send another attachment for download.

A project was created some time ago. The attachment can be downloaded directly with a mark to find the file. However, every time you open a TXT file, the image is displayed. Are opened directly in a browser. Let's talk about all the tests. If you do what you do, I just open it. There is no way to download the file (right-click a custom menu). I said it was your system problem. Shut down my computer .. People say, you can all look at others... Then I can't afford it .. There is such a small piece of code.

In other words, testing and development are a natural enemy. Good code: Nothing happens locally. Once we get to the test environment, we will talk to them about various amazing bugs. Every time we talk to them, we will clear the cache and clear the cache... I hate testing.

In fact, the simple code is to add a global hanlder.


1 using system; 2 using system. collections. generic; 3 using system. io; 4 using system. LINQ; 5 using system. text; 6 using system. web; 7 8 namespace Wanda. siCi. UI. web 9 {10 public class attachmentdownloadhanlder: ihttphandler11 {12 public void processrequest (httpcontext context) 13 {14 string Path = context. request. physicalpath; 15 16 try17 {18 context. response. charset = "UTF-8"; 19 context. response. contentencoding = encoding. utf8; 20 context. response. headerencoding = encoding. utf8; 21 context. response. appendheader ("content-disposition", 22 "attachment; filename =" + 23 httputility. urlencode (path. getfilename (PATH), encoding. utf8); 24 context. response. writefile (PATH); 25} 26 catch (exception ex) 27 {28 context. response. write ("download file error, cause:" + ex. message); 29} 30 finally31 {32 context. response. end (); 33} 34} 35 36 Public bool isreusable {get {return false;} 37} 38}


Then add a configuration file, which can be placed in the root directory of your attachment.


Web. config

 1 <?xml version="1.0" encoding="utf-8"?> 2 <configuration> 3   <system.web> 4     

If an error is reported, change % WinDir % \ Microsoft. NET \ framework64 \ v4.0.30319 \ aspnet_isapi.dll

% Windir % \ Microsoft. NET \ framework \ v4.0.30319 \ aspnet_isapi.dll.

At the beginning, I kept 500. Later I checked what I said to enable the 32-bit simulation of IIS.

In this way, the attachment will be marked for each download and will not be opened in the browser.

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: 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.