利用ASP.NET的WebClient來Download遠端File
最近看到小舖的一篇討論....
讓小弟產生一個idea,就是透過WebClient來下載遠端的任何檔檔
然後出現下載視窗讓使用者另儲新檔...
這樣在網路看到別人的圖片,Flash就可以下載回來了...(但要注意別侵權喔..)
c#範例
RemoteFileDownload.aspx
01 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="RemoteFileDownload.aspx.cs"
02 Inherits="RemoteFileDownload" %>
03
04 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
05 <html xmlns="http://www.w3.org/1999/xhtml">
06 <head id="Head1" runat="server">
07 <title>RemoteFileDownload</title>
08 </head>
09 <body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:Label ID="Label1" runat="server" Text="Url:"></asp:Label>
13 <asp:TextBox ID="TextBox1" runat="server" Width="300px">http://www.blueshop.com.tw/images/bs_logo.gif</asp:TextBox>
14 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="download" /></div>
15 </form>
16 </body>
17 </html>
18
02 Inherits="RemoteFileDownload" %>
03
04 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
05 <html xmlns="http://www.w3.org/1999/xhtml">
06 <head id="Head1" runat="server">
07 <title>RemoteFileDownload</title>
08 </head>
09 <body>
10 <form id="form1" runat="server">
11 <div>
12 <asp:Label ID="Label1" runat="server" Text="Url:"></asp:Label>
13 <asp:TextBox ID="TextBox1" runat="server" Width="300px">http://www.blueshop.com.tw/images/bs_logo.gif</asp:TextBox>
14 <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="download" /></div>
15 </form>
16 </body>
17 </html>
18
RemoteFileDownload.aspx.cs
01 using System;
02 using System.Data;
03 using System.Configuration;
04 using System.Collections;
05 using System.Web;
06 using System.Web.Security;
07 using System.Web.UI;
08 using System.Web.UI.WebControls;
09 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 public partial class RemoteFileDownload : System.Web.UI.Page
13 {
14 protected void Page_Load(object sender, EventArgs e)
15 {
16
17 }
18 protected void Button1_Click(object sender, EventArgs e)
19 {
20 System.Net.WebClient net = new System.Net.WebClient();
21 string link = this.TextBox1.Text;
22 Response.ClearHeaders();
23 Response.Clear();
24 Response.Expires = 0;
25 Response.Buffer = true;
26 Response.AddHeader("Accept-Language", "zh-tw");
27 Response.AddHeader("Content-Disposition", "Attachment;FileName=" + System.Web.HttpUtility.UrlEncode(link, System.Text.Encoding.UTF8));
28 Response.ContentType = "APPLICATION/octet-stream";
29 Response.BinaryWrite(net.DownloadData(link));
30 Response.End();
31 }
32 }
02 using System.Data;
03 using System.Configuration;
04 using System.Collections;
05 using System.Web;
06 using System.Web.Security;
07 using System.Web.UI;
08 using System.Web.UI.WebControls;
09 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11
12 public partial class RemoteFileDownload : System.Web.UI.Page
13 {
14 protected void Page_Load(object sender, EventArgs e)
15 {
16
17 }
18 protected void Button1_Click(object sender, EventArgs e)
19 {
20 System.Net.WebClient net = new System.Net.WebClient();
21 string link = this.TextBox1.Text;
22 Response.ClearHeaders();
23 Response.Clear();
24 Response.Expires = 0;
25 Response.Buffer = true;
26 Response.AddHeader("Accept-Language", "zh-tw");
27 Response.AddHeader("Content-Disposition", "Attachment;FileName=" + System.Web.HttpUtility.UrlEncode(link, System.Text.Encoding.UTF8));
28 Response.ContentType = "APPLICATION/octet-stream";
29 Response.BinaryWrite(net.DownloadData(link));
30 Response.End();
31 }
32 }