2014-04-09 98 views
0
var graphids = from o in db.OfflineCarts 
           join i in db.Graphs on o.ItemId equals i.ItemUserID 
           select i; 

      GridView gv = new GridView(); 
      gv.DataSource = graphids.ToList(); 
      gv.DataBind(); 
      Response.ClearContent(); 
      Response.Buffer = true; 
      Response.AddHeader("content-disposition", "attachment; filename=GraphTable.xls"); 
      Response.ContentType = "application/ms-excel"; 
      Response.Charset = ""; 
      StringWriter sw = new StringWriter(); 
      HtmlTextWriter htw = new HtmlTextWriter(sw); 
      gv.RenderControl(htw); 
      Response.Output.Write(sw.ToString()); 
      Response.Flush(); 
      Response.End(); 
      Encryption(); 


    public void Encryption() 
    { 

     string password = @"myKey123"; // Your Key Here 
     UnicodeEncoding UE = new UnicodeEncoding(); 
     byte[] key = UE.GetBytes(password); 

     string cryptFile = "/Users/Neeraj/Downloads/UserFilesEncrypt.xls"; 
     FileStream fsCrypt = new FileStream(cryptFile, FileMode.CreateNew); 

     RijndaelManaged RMCrypto = new RijndaelManaged(); 
     CryptoStream rs = new CryptoStream(fsCrypt, 
      RMCrypto.CreateEncryptor(key, key), 
      CryptoStreamMode.Read); 

     CryptoStream cs = new CryptoStream(fsCrypt, 
      RMCrypto.CreateEncryptor(key, key), 
      CryptoStreamMode.Write); 

     string inputFile = "/Users/Neeraj/Downloads/GraphTable.xls"; 
     FileStream fsIn = new FileStream(inputFile, FileMode.Open); 

     int data; 
     while ((data = fsIn.ReadByte()) != -1) 
      cs.WriteByte((byte)data); 

     fsIn.Close(); 
     cs.Close(); 
     fsCrypt.Close(); 

    } 

我加密文件,它是由網格視圖中mvc5下載剛過瞬間,但我得到0字節的加密文件,當我重新下載文件,比我得到適當的加密文件可能有人告訴我,什麼是錯的代碼或我必須下載後推遲加密功能的某個時候文件加密

回答

1

使用System.Security

FileStream fsInput = new FileStream(sInputFilename, 
       FileMode.Open, 
       FileAccess.Read); 

FileStream fsEncrypted = new FileStream(sOutputFilename, 
       FileMode.Create, 
       FileAccess.Write);