Silverlight怎么实现文件下载啊

萬里無雲 2009-09-18 10:16:16
文件上传没啥问题,就用WebClient来实现了,可是下载,看了些sample都是下载图片,然后让图片直接显示在页面中的,如下:

private void btnDownload_Click(object sender, RoutedEventArgs e)
{
//向指定的Url发送下载流数据请求
String imgUrl = "http://localhost/Pics/aa.jpg";

Uri endpoint = new Uri(imgUrl);

WebClient client = new WebClient();
client.OpenReadCompleted += new OpenReadCompletedEventHandler(OnOpenReadCompleted);
// client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(clientDownloadStream_DownloadProgressChanged);
client.OpenReadAsync(endpoint);
}
void OnOpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{

//OpenReadCompletedEventArgs.Error - 该异步操作期间是否发生了错误
//OpenReadCompletedEventArgs.Cancelled - 该异步操作是否已被取消
//OpenReadCompletedEventArgs.Result - 下载后的 Stream 类型的数据
//OpenReadCompletedEventArgs.UserState - 用户标识

if (e.Error != null)
{
MessageBox.Show(e.Error.ToString());
return;
}
if (e.Cancelled != true)
{
// 获取下载的流数据(在此处是图片数据)并显示在图片控件中
Stream stream = e.Result;
BitmapImage bitmap = new BitmapImage();
bitmap.SetSource(stream);
imgDownLoad.Source = bitmap;

}

}

怎么没有直接弹出保存对话框下载的?

谁有sample给一个看看啊,或者给点思路,谢谢啊。
...全文
2476 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
diy2005 2011-10-23
  • 打赏
  • 举报
回复
NavigateUri= "http://222.80.0.50/ClientBin/

服务器路径怎么写?
mashurong 2011-09-18
  • 打赏
  • 举报
回复
学习一下,多谢了
keke811 2010-11-29
  • 打赏
  • 举报
回复
进来逛下,我也在做上传,下载.
haolei417 2009-11-03
  • 打赏
  • 举报
回复
不做成脱浏览器的话
调用页面脚本也行
HtmlPage.Window.Eval("脚本方法(参数)")

dreamcatcher001 2009-10-06
  • 打赏
  • 举报
回复
多谢,我也用到下载了
萬里無雲 2009-09-18
  • 打赏
  • 举报
回复
成功了,在HyperlinkButton里加了一个TargetName="_self"就可以了,谢谢各位!
萬里無雲 2009-09-18
  • 打赏
  • 举报
回复
现在只要能成功请求到http://localhost/WebClientDownHandler.ashx?filename= 这个地址应该就可以了,可是怎么请求呀呀呀
萬里無雲 2009-09-18
  • 打赏
  • 举报
回复
这个文章我也看到了,并已经参考上述文章改写为C#版本,如下,直接在浏览器输入http://localhost/WebClientDownHandler.ashx?fileName=aa.txt 可以实现下载,
但是给HyperlinkButton 设置NavigateUri却不可以,报错误大概是说NavigateUri只支持相对路径,或者以/开始。

我又尝试点按钮时这样来做,结果点了和用WebClient 一样啥反应也没有
Uri upTargetUri = new Uri(String.Format("http://localhost/WebClientDownHandler.ashx?filename={0}", imgUrl), UriKind.Absolute); //指定上传地址
WebRequest request = WebRequest.Create(upTargetUri);
request.Method = "GET";
request.ContentType = "application/octet-stream";
request.BeginGetResponse(new AsyncCallback(RequestReady), request);

为什么啊???为什么啊???
为什么啊???
为什么啊???



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

namespace USTCORi.WebLab.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 = context.Request.QueryString["filename"];

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

}

zhuzhi 2009-09-18
  • 打赏
  • 举报
回复
参考http://www.cnblogs.com/wmt1708/archive/2009/03/07/1405009.html

xaml采用LinkButton 其NavigateUri属性链接到服务端下载的ashx文档



<HyperlinkButton Margin="32,135,198,140" Content="HyperlinkButton" NavigateUri="http://localhost:1399/download.ashx" />

1Imports System.Web
2Imports System.Web.Services
3Imports System.Net
4Public Class downloadClass download
5 Implements System.Web.IHttpHandler
6
7 Private ChunkSize As Long = 102400 '100K 每次读取文件,只读取100K,这样可以缓解服务器的压力
8
9 Sub ProcessRequest()Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
10 Dim fileName As String = "aaa.JPG" '客户端保存的文件名
11 Dim filePath As String = context.Server.MapPath("image/IMG_1370.JPG") '路径
12
13 Dim fileInfo As System.IO.FileInfo = New System.IO.FileInfo(filePath)
14
15 If fileInfo.Exists = True Then
16
17 Dim buffer(ChunkSize) As Byte
18
19 context.Response.Clear()
20 Dim iStream As System.IO.FileStream = System.IO.File.OpenRead(filePath)
21 Dim dataLengthToRead As Long = iStream.Length '获取下载的文件总大小
22
23 context.Response.ContentType = "application/octet-stream"
24 '通知浏览器下载文件而不是打开
25 context.Response.AddHeader("Content-Disposition", "attachment; filename=" & _
26 HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8))
27Loop1:
28 While dataLengthToRead > 0 And context.Response.IsClientConnected
29 Dim lengthRead As Integer = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize)) '读取的大小
30 context.Response.OutputStream.Write(buffer, 0, lengthRead)
31 context.Response.Flush()
32 dataLengthToRead = dataLengthToRead - lengthRead
33 End While
34
35 context.Response.Close()
36 context.Response.End()
37 End If
38
39 'context.Response.ContentType = "text/plain"
40 'context.Response.Write("Hello World!")
41
42 End Sub
43
44 ReadOnly Property IsReusable()Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
45 Get
46 Return False
47 End Get
48 End Property
49
50End Class
jv9 2009-09-18
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 liyun919 的回复:]
成功了,在HyperlinkButton里加了一个TargetName="_self"就可以了,谢谢各位!
[/Quote]

标记一下,很好的经验。
xingjunli 2009-09-18
  • 打赏
  • 举报
回复
建议:另存为下载还是从服务器来处理,Silverlight只是请求一个URL吧
zhuzhi 2009-09-18
  • 打赏
  • 举报
回复
<object id="slPlugin1" width="300" height="50"
data="data:application/x-silverlight-2,"
type="application/x-silverlight-2" >
<param name="source" value="ClientBin/SilverlightApplication.xap"/>
<param name="initParams"
value="id=slPlugin1,embeddingTechnique=objectElement"/>
<param name="enableNavigation" value="all"/>

</object>
zhuzhi 2009-09-18
  • 打赏
  • 举报
回复
HyperlinkButton报错是因为你没有设置EnableNavigation属性
EnableNavigation属性
获取或设置一个值,该值指示 Silverlight 插件中承载的内容是否可以使用 HyperlinkButton 来导航到外部 URI。

<object id="slPlugin1" width="300" height="50"
data="data:application/x-silverlight-2,"
type="application/x-silverlight-2" >
<param name="source" value="ClientBin/SilverlightApplication.xap"/>
<param name="initParams"
value="id=slPlugin1,embeddingTechnique=objectElement"/>
<!-- Installation HTML omitted. -->
</object>


8,737

社区成员

发帖
与我相关
我的任务
社区描述
WPF/Silverlight相关讨论
社区管理员
  • WPF/Silverlight社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧