ASP. net File download

Source: Internet
Author: User


The. aspx code is as follows:

<%@ page title= "" Language= "C #" masterpagefile= "~/sample_code/amst_sample.master" autoeventwireup= "true" CodeFile = "S6_downloadLink.aspx.cs" inherits= "Sample_code_s8_downloadlink"%><asp:content id= "Content1" Contentplaceholderid= "Cph_head" runat= "Server" ></asp:content><asp:content id= "Content2" Contentplaceholderid= "Cph_memo" runat= "Server" > <ul> <li> Normal hyperlinks <a> sometimes not enough to meet the needs of the download, but the browser opens the text directly Pieces </li> </ul></asp:content><asp:content id= "Content3" contentplaceholderid= "Cph_main" Runat= " Server > <p> Normal hyperlink link a picture file [direct Open]: <a href= ". /imgs/img1.jpg "> <a href=". /imgs/img1.jpg "/> </a></p> <p> normal hyperlink link a rar file [download]: <a href=". /imgs/temp.rar "> <a href=". /imgs/temp.rar "/> </a> </p> <p> Normal hyperlink to a WAV file [the plug-in or run environment is different and decided to open or download]: <a href=". /imgs/music1.wav "> <a href=". /imgs/music1.wav "/> &Lt;/a></p> <p> Pictures <a href= "with hyperlinks with [download] Properties". /imgs/img1.jpg "download=" myimg.jpg "> Picture download </a> </p> <p style=" FONT-SIZE:16PX; color:red; " > completely does not expose the download of the target file LinkButton <asp:linkbutton id= "LinkButton1" runat= "Server" onclick= "LinkButton1_Click" > Picture file Download </asp:LinkButton> </p></asp:Content>

The. Aspx.cs code is as follows:

Using system;using system.collections.generic;using system.linq;using system.web;using System.Web.UI;using System.web.ui.webcontrols;public partial class sample_code_s8_downloadlink:system.web.ui.page{    protected void Page_Load (object sender, EventArgs e)    {    }    protected void LinkButton1_Click (object sender, EventArgs e)    {        String fileName = "Tempimg.jpg";        String filePath = Server.MapPath ("~/imgs/img3.jpg");        Response.Clear ();        Response.Buffer = true;        Response.AddHeader ("Content-disposition", "attachment;filename=" + filename);        Response.ContentType = "Application/unknow";        Response.TransmitFile (FilePath);        Response.End ();    }}

The results are as follows:


ASP. net File download

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.