Windowsphone7 File Download Method

Source: Internet
Author: User

There are multiple methods to download files in WP7, such as using httpwebrequest, WebClient, or even background processes to download files. For files such as images supported by WP, you can also bind a data source to the control for automatic download.

The method of using WebClient can refer to this better example: http://www.cnblogs.com/linzheng/archive/2011/11/03/2234971.html

Example of using background processes to upload and download files: http://www.cnblogs.com/webabcd/archive/2012/07/16/2592989.html

For images, You can bind a remote URI to automatically download the image and display it after the system is complete. For example:

<Image Source="http://xxx.com/xxx/remoteImage.png" Stretch="None" />

If you want to display a default image before the image download is complete, and then update the image after the download is complete, you can listen to this to complete the download. For how to implement it, refer to the following code:

Bitmapimage bitmap = new bitmapimage (); bitmap. createoptions = bitmapcreateoptions. none; // when the image is downloaded and decoded successfully, the imageopened bitmap is triggered. imageopened + = (S, e) =>{// display the image in the control userheadimage. The control has defined userheadimage in XAML. source = s as bitmapimage;}; // URI is the remote file address bitmap. urisource = new uri (URI );


The last thing worth thinking about is that the client may implement the file caching mechanism and want to know if the network file has been changed. It is necessary to download the file only when the client has been changed, otherwise, the traffic waste caused by repeated downloads is avoided. In a solution, a simple request is sent before the file is downloaded, requesting the server to send back the last modification time and other information of the file. At the same time, the client will save the last update time of the file, and then compare the two times. If the server time is newer, download the file; otherwise, the locally cached file will continue to be used. This imitates the browser's page loading technology.

The following describes how to send a WebClient request and obtain the last modification time of a file in the header information returned by the server:

WebClient request = new WebClient(); request.OpenReadAsync(new Uri(url)); request.OpenReadCompleted += (s, e) => {     string lastModifiedDate = request.ResponseHeaders["Last-Modified"]; };

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.