日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Silverlight实现文件下载

發(fā)布時間:2023/12/10 编程问答 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Silverlight实现文件下载 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

首先在service層建一個Handler,內(nèi)容如下:

?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace test.Service
{
?????? public class WebClientDownHandler : IHttpHandler
??? {
??????

?
??????? public void ProcessRequest(HttpContext context)
??????? {

??????????? String fileName = context.Request.QueryString["filename"]; //客戶端保存的文件名
??????????? String filePath = context.Server.MapPath("Pics/" + fileName); //路徑

??????????? //string fileName = "ChartImg.png.jpg";

??????????? FileInfo fileInfo = new FileInfo(filePath);

??????????? if (fileInfo.Exists)
??????????? {

???????????????

??????????????? byte[] buffer = new byte[102400];
??????????????? context.Response.Clear();

??????????????? FileStream iStream = File.OpenRead(filePath);
??????????????? long dataLengthToRead = iStream.Length; //獲取下載的文件總大小

??????????????? context.Response.ContentType = "application/octet-stream";
??????????????? context.Response.AddHeader("Content-Disposition", "attachment;? filename=" +
?????????????????????????????????? HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));
??????????????? while (dataLengthToRead > 0 && context.Response.IsClientConnected)
??????????????? {
??????????????????? int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(102400));//'讀取的大小

??????????????????? context.Response.OutputStream.Write(buffer, 0, lengthRead);
??????????????????? context.Response.Flush();
??????????????????? dataLengthToRead = dataLengthToRead - lengthRead;
??????????????? }
??????????????? context.Response.Close();
??????????????? context.Response.End();

??????????? }

????????
??????? }

?


??????? public bool IsReusable
??????? {
??????????? get
??????????? {
??????????????? return false;
??????????? }
??????? }
??? }

}
假如我們的service的Pics目錄下有一個文件test.doc

我們可以試試http://localhost/WebClientDownHandler.ashx?filename=test.doc?看看能不能訪問下載文件

?

?

如果成功了,在Silverlight頁面直接給HyperlinkButton指定NavigateUri為上述url就可以了,注意可能需要給HyperlinkButton加上TargetName="_self"

總結(jié)

以上是生活随笔為你收集整理的Silverlight实现文件下载的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。