您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 海北分类信息网,免费分类信息发布

SilverLight下载任意文件技巧

2025/2/15 3:15:40发布7次查看
xaml采用linkbutton 其navigateuri属性链接到服务端下载的ashx文档 如 hyperlinkbutton margin=32,135,198,140 content=hyperlinkbutton navigateuri=http://localhost:1399/download.ashx / download.ashx 如下 code 1 imports system.web 2 imports
xaml采用linkbutton 其navigateuri属性链接到服务端下载的ashx文档

download.ashx如下
code
 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
海北分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录