Tips for downloading arbitrary files using SilverLight

Source: Internet
Author: User
Tags silverlight

Xaml uses the LinkButton's NavigateUri attribute to link to the ashx document downloaded by the server.

For example

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

 

Download. ashxAs follows:

 

Code
1 Imports System. Web
2 Imports System. Web. Services
3 Imports System. Net
4 Public Class downloadClass download
5 Implements System. Web. IHttpHandler
6
7 Private ChunkSize As Long = 102400 '100kb each time the file is read, only 100 KB is read, which can relieve the pressure on the server.
8
9 Sub ProcessRequest () Sub ProcessRequest (ByVal context As HttpContext) Implements IHttpHandler. ProcessRequest
10 Dim fileName As String = "aaa. JPG" 'file name saved by the client
11 Dim filePath As String = context. Server. MapPath ("image/IMG_1370.JPG") 'path
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' get the total size of the downloaded file
22
23 context. Response. ContentType = "application/octet-stream"
24' notify the browser to download the file instead of opening it
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) 'Read size
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 () As Boolean Implements IHttpHandler. IsReusable
45 Get
46 Return False
47 End Get
48 End Property
49
50End Class
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.