wp8通过WebClient从服务器下载文件
来源:互联网 发布:超市收银软件多少钱 编辑:程序博客网 时间:2024/05/17 05:11
通过WebClient从Web服务器下载文件,并保存到wp8手机应用程序的独立存储。
我们可以通过利用webClient_DownloadStringCompleted来获得下载完成所需要的时间,用Stopwatch得到下载的总时间。
通常我们都将上传、下载作为异步事件来处理,以便不阻止主线程。
String url = "http://172.18.144.248:8080/upload/" + filename;WebClient client = new WebClient();client.Encoding = System.Text.Encoding.UTF8;client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(this.webClient_DownloadStringCompleted);Stopwatch sw;sw = Stopwatch.StartNew();//用来记录总的下载时间public static Task<string> DownloadStringTask(this WebClient webClient, Uri uri){ var tcs = new TaskCompletionSource<string>(); webClient.DownloadStringCompleted += (s, e) => { if (e.Error != null) { tcs.SetException(e.Error); } else { tcs.SetResult(e.Result); } }; webClient.DownloadStringAsync(uri); return tcs.Task;}public void webClient_DownloadStringCompleted(object s, DownloadStringCompletedEventArgs e){ sw.Stop(); long totaltime = sw.ElapsedMilliseconds; MessageBox.Show(totaltime+"ms,download succeed!");}webClient.DownloadStringAsync(uri)得到的是字符串,我们将其保存到独立存储中,通过 await IsolatedStorageHelper.writeFileAsync(filename,cont)异步执行,通过writeFileAsync函数写入独立存储。
public async static Task writeFileAsync(String fileName, String text) { using (IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) { if (isolatedStorageFile.FileExists(fileName)) { isolatedStorageFile.DeleteFile(fileName); } using (IsolatedStorageFileStream isolatedStorageFileStream = isolatedStorageFile.CreateFile(fileName)) { using (StreamWriter streamWriter = new StreamWriter(isolatedStorageFileStream,System.Text.Encoding.UTF8)) { streamWriter.Write(text); streamWriter.Close(); } isolatedStorageFileStream.Close(); isolatedStorageFileStream.Dispose(); } } }
1 0
- wp8通过WebClient从服务器下载文件
- 从其他服务器上下载数据.通过webclient
- 使用WebClient从FTP服务器下载文件…
- webClient 从网络下载文件
- 下载服务器文件至客户端/WebClient上传文件至服务器
- WinFrom通过WebClient向Web服务器上传下载文件
- WebClient下载文件代码!
- WebClient下载文件
- C#下载文件:WebClient
- WebClient异步下载文件
- webclient下载文件
- 通过Xshell如何从Linux服务器下载文件
- WebClient 从网上下载图片
- 利用WebClient异步下载文件
- 使用WebClient实现文件下载
- C# 文件下载(WebClient)
- 使用WebClient实现文件下载
- 使用WebClient实现文件下载
- Impala调试注意事项
- 使用Git进行工作文档的版本控制
- 固定层
- javascript绘制静态或者动态的图表、关系表、流程图-JointJS
- 华为面试题:通过交换元素,使两个数组的元素和之差最小
- wp8通过WebClient从服务器下载文件
- 九度OJ做题记录 更新.....
- Android中dpi,dp(dip),px关系,与计算公式
- bmob 云端代码笔记
- Android学习之Context
- C++学习:关于“std::vector<Type>::iterator”的一个错误
- asp.net 右下角弹出新提醒信息提示框
- test
- 实现一个类似的python消息响应机制