前端:

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="RemoteFileDownload.aspx.cs" Inherits="RemoteFileDownload" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>RemoteFileDownload</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Url:"></asp:Label>
        <asp:TextBox ID="TextBox1" runat="server" Width="300px">http://www.blueshop.com.tw/images/bs_logo.gif</asp:TextBox>
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="download" /></div>
    </form>
</body>
</html>

後端:

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class RemoteFileDownload : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        System.Net.WebClient net = new System.Net.WebClient();
        string link = this.TextBox1.Text;      
        Response.ClearHeaders();
        Response.Clear();
        Response.Expires = 0;
        Response.Buffer = true;
        Response.AddHeader("Accept-Language", "zh-tw");
        Response.AddHeader("Content-Disposition", "Attachment;FileName=" + System.Web.HttpUtility.UrlEncode(link, System.Text.Encoding.UTF8));
        Response.ContentType = "APPLICATION/octet-stream";
        Response.BinaryWrite(net.DownloadData(link));
        Response.End();
    }
}

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 alen1985 的頭像
    alen1985

    1與0的世界

    alen1985 發表在 痞客邦 留言(0) 人氣()