private async void OnCommandAct(IUICommand command) { int cmdId = (int)command.Id; if (cmdId == 1) { StorageFile file = null; StorageFolder storagefolder = KnownFolders.RemovableDevices; int flag = 0; foreach (var f in await storagefolder.GetFoldersAsync()) { foreach (var SDfolder in await f.GetFoldersAsync()) { if (SDfolder.Name == "Downloads") { file = await SDfolder.CreateFileAsync("screenimg.jpg", CreationCollisionOption.ReplaceExisting); File.WriteAllBytes(file.Path, writebuffer); await new MessageDialog("图片已保存").ShowAsync(); flag = 1; break; } } if (flag == 0) { file = await f.CreateFileAsync("screenimg.jpg", CreationCollisionOption.ReplaceExisting); File.WriteAllBytes(file.Path, writebuffer); break; } } } }
//下载文件,接收文件信息 public async static void ReceiveFile(DiskItemModel diskitem, object obj) { StreamSocket stream = obj as StreamSocket; int BUFFERSZEINT; byte[] bufferlength = new byte[sizeof(Int32)]; byte[] filebuffer; byte[] temp = null; byte[] recvfilebuffer = new byte[0]; DataReader reader = new DataReader(stream.InputStream); reader.InputStreamOptions = InputStreamOptions.Partial; //异步读取 try { uint count = await reader.LoadAsync(sizeof(Int32)); //从流中加载一个long长度的数据 reader.ReadBytes(bufferlength); //获取所加载的long长度的数据 //await Task.Delay(1000); //文件长度 int fileLength = BitConverter.ToInt32(bufferlength, 0); App.totaldownload = fileLength; Debug.WriteLine("接收到的文件大小: " + fileLength); //文件接收剩余长度 int notRecFileLength = fileLength; int len = fileLength / (1024 * 1024 * 1); BUFFERSZEINT = SetBUFFERSIZE(len); filebuffer = new byte[BUFFERSZEINT]; //在非ui线程中一定不要进行ui操作,容易发生线程冲突,从而形成Access is denied ,未捕获的异常 //await new MessageDialog("文件开始下载,请等待").ShowAsync(); //当接收文件的长度小于文件长度 while (recvfilebuffer.Length < fileLength) { //最后一次少于最多接满缓冲区 if (notRecFileLength < BUFFERSZEINT) { count = await reader.LoadAsync((uint)notRecFileLength); App.predownload = App.alreadydownload; App.alreadydownload += (int)count; notRecFileLength -= (int)count; temp = new byte[count]; } //前几次,每次缓冲区都为最大 else { //filebuffer = new byte[BUFFERSZEINT]; //实际接收的流长度为count count = await reader.LoadAsync((uint)BUFFERSZEINT); App.predownload = App.alreadydownload; App.alreadydownload += (int)count; //未接收文件的长度 notRecFileLength -= (int)count; temp = new byte[count]; } reader.ReadBytes(temp); //alreadydownload +=(int)count; //fileinfo.Count = alreadydownload; //获取所加载的数据以字节数组的形式 //将已接收的文件流存起来 recvfilebuffer = recvfilebuffer.Concat(temp).ToArray(); //下载完毕 if (recvfilebuffer.Length >= fileLength) { //GetFile(diskitem.FullName,fileLength,"下载成功"); App.downloadfile.RemoveAt(0); } } reader.DetachStream(); byte[] writebuffer = new byte[fileLength]; writebuffer = GetSendBuffer(writebuffer, recvfilebuffer); if (!diskitem.Name.EndsWith(".jpg") && !diskitem.Name.EndsWith(".jpeg") && !diskitem.Name.EndsWith(".png") && !diskitem.Name.EndsWith(".3gp") && !diskitem.Name.EndsWith(".mp4") && !diskitem.Name.EndsWith(".mp3") && !diskitem.Name.EndsWith(".wma") && !diskitem.Name.EndsWith(".avi") && !diskitem.Name.EndsWith(".mov") && !diskitem.Name.EndsWith(".amr")) { App.txtstring = Encoding.UTF8.GetString(writebuffer); } else { StorageFile file = null; StorageFolder storagefolder = KnownFolders.RemovableDevices; int flag = 0; foreach (var f in await storagefolder.GetFoldersAsync()) { foreach (var SDfolder in await f.GetFoldersAsync()) { if (SDfolder.Name == "Downloads") { file = await SDfolder.CreateFileAsync(diskitem.Name, CreationCollisionOption.ReplaceExisting); File.WriteAllBytes(file.Path, writebuffer); //Task t = Task.Factory.StartNew( new Action(async ()=> { // await new MessageDialog("文件下载完成").ShowAsync(); //})); flag = 1; break; } } if (flag == 0) { file = await f.CreateFileAsync(diskitem.Name, CreationCollisionOption.ReplaceExisting); File.WriteAllBytes(file.Path, writebuffer); break; } //在download文件夹中保存成功,下载下一个文件。 else if (flag == 1) { //待下载队列不为空,下载 if (App.downloadfile.Count > 0) { DownloadNextFile(); } break; } } } } catch (Exception e) { Debug.WriteLine(e.Message); //await new MessageDialog(e.Message).ShowAsync(); reader.DetachStream(); App.downloadfile.RemoveAt(0); if (App.downloadfile.Count > 0) { DownloadNextFile(); } //stream.Dispose(); return; } }