zoukankan      html  css  js  c++  java
  • 文件和流的相互转化

     1 public class UploadFile
     2 {
     3     //文件名称
     4     public string FileName{get;set;}
     5     //文件类型
     6     public StorageFile File{get;set;}
     7     
     8     //文件数据流
     9    public Stream FileStream { get; private set; }
    10    
    11    
    12     public UploadFile(StorageFile file)
    13     {
    14         this.StorageFile=file.Name;
    15         this.File=file;
    16     }
    17     
    18     //将文件转换成流
    19     public async Task<byte[]> GetBytes()
    20     {
    21         FileStream =(Stream) await File.OpenStreamForReadAsync();
    22         byte[] bytes = new byte[FileStream.Length];
    23         FileStream.Read(bytes, 0, bytes.Length);
    24         FileStream.Seek(0, SeekOrigin.Begin);
    25         return bytes;
    26     }
    27     
    28     
    29     
    30     //将流写入文件
    31        public async void GetFile(Byte[] bytes)
    32         { 
    33             StorageFolder applicationFolder = ApplicationData.Current.LocalFolder;
    34             StorageFile storageFile = await applicationFolder.CreateFileAsync("1.txt", CreationCollisionOption.ReplaceExisting);
    35             StorageFile file = await applicationFolder.GetFileAsync("1.txt");
    36             using (var stream = await file.OpenStreamForWriteAsync())
    37             {
    38                 await stream.WriteAsync(bytes, 0, bytes.Length);
    39                 stream.Flush();
    40             }
    41 
    42     
    43 }
  • 相关阅读:
    MVC1
    Linux中的软连接与硬连接
    python之multiprocessing(二):multiprocessing.Pool
    python之multiprocessing(一)
    python之paramiko(一)
    python_Exception之:TypeError: 'int' object is not iterable
    python之socket(一)
    python之pymysql(一)
    生成树协议--STP
    路由协议--RIP
  • 原文地址:https://www.cnblogs.com/lj940306/p/4681140.html
Copyright © 2011-2022 走看看