asp.net檔案下載

來源:互聯網
上載者:User

標籤:asp.net   檔案下載   


.aspx代碼如下:

<%@ 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>普通的超連結 <a>有時無法滿足下載的需求,而是用瀏覽器直接開啟了檔案</li>    </ul></asp:Content><asp:Content ID="Content3" ContentPlaceHolderID="CPH_MAIN" Runat="Server">    <p>普通超連結連結一個圖片檔案[直接開啟]:        <a href="../imgs/img1.jpg">           <a href="../imgs/img1.jpg"/>    </a></p>    <p>普通超連結連結一個rar檔案[下載]:        <a href="../imgs/temp.rar">             <a href="../imgs/temp.rar"/>        </a>    </p>    <p>普通超連結指向一個wav檔案[外掛程式或運行環境不同而決定是開啟還是下載]:    <a href="../imgs/music1.wav">         <a href="../imgs/music1.wav"/>    </a></p>    <p>帶有[download]屬性的超連結-> 圖片         <a href="../imgs/img1.jpg" download="myImg.jpg">圖片下載</a>    </p>    <p style="font-size:16px; color:Red;">完全不暴露目標檔案的下載LinkButton        <asp:LinkButton ID="LinkButton1" runat="server" onclick="LinkButton1_Click">圖片檔案下載</asp:LinkButton>    </p></asp:Content>

.aspx.cs代碼如下:

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();    }}

運行效果如下:


asp.net檔案下載

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.