title: Corrupted Excel file after downloading with C# DownloadFile()
body: <p>I am making a SSIS package that is downloading files from a website. In order to get the right links, I have store a &quot;Full result Set&quot; (Object Variable), which is then using the &quot;For each loop container&quot; to run through the code (as a String variable).</p>
  <p>Example url: https://website/data/date1.aspx. If I open this link, it is automatically downloading the excel file date1.xlsx.</p>
  <p>Till now I have made the following C# code:</p>
  <pre><code>public void Main()
      {
          
          string testfile = Dts.Variables[&quot;User::filename2&quot;].Value.ToString();
         
  
          string[] arr = new string[] {testfile};
         
          try
          {
              string myURI = &quot;https://website/data/&quot;;
              string myWebString = null, myLocalPath = null;
              WebClient myWebClient = new WebClient();
              foreach (string fileName in arr)
              {
                  myWebString = myURI + fileName;
                  myLocalPath = &quot;c:\\sql\\&quot; + fileName+&quot;.xlsx&quot;;
                  myWebClient.DownloadFile(myWebString, myLocalPath);
              }
              Dts.TaskResult = (int)ScriptResults.Success;
          }
          catch (Exception ex)
          {
              Dts.Events.FireError(18, &quot;The process failed&quot;, ex.ToString(), &quot;&quot;, 0);
              Dts.TaskResult = (int)ScriptResults.Failure;
          }
          Dts.TaskResult = (int)ScriptResults.Success;
      }
  
      #region ScriptResults declaration
      /// &lt;summary&gt;
      /// This enum provides a convenient shorthand within the scope of this class for setting the
      /// result of the script.
      /// 
      /// This code was generated automatically.
      /// &lt;/summary&gt;
      enum ScriptResults
      {
          Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
          Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
      };
      #endregion
  
  }
  </code></pre>
  <p>The variables I am using are &quot;date1.aspx&quot;, &quot;date2.aspx&quot; etc...
  The files now automatically downloaded and stored as &quot;date1.aspx.xlsx&quot; and &quot;date2.aspx.xlsx&quot;.</p>
  <p>However when I open the excel files I see that the files are corrupted.
  But when I import it with Data&gt;From File Excel, excel is able to open the files correctly.</p>
  <p>I think there I something wrong with my code. I have no background in C# so I am really doing trial and error method for some days now. Eventually I want to work with the files in the SSIS package, so It would be great to store them in CSV file format directly, because also the &quot;sheet names&quot; in excel will change every time, so it is hard for me to make a &quot;for each loop&quot;, because then I need additional code again.</p>
  <p>Would be great if somebody is able to help me! Thank you!</p>
  <p>The excel file look in notepad like this:</p>
  <pre class="lang-none prettyprint-override"><code>‡    ,  —  ±  3  M  ¿  Ù  F
    `  Ó
    í  *  D  ¾  Ø  +  E     ©  Ã    A  [
    £  ½
      %  š  ´    *  }  —  â  ü  H  b
    î    l  †  
    $  ¡  »    9  ª  Ä  6  P  º  Ô  L  f  ä  þ  h  ‚  â  ü  T  n  è    s    â  ü  r  Œ  ü    ‰  £  +  E    ©    &amp;  ”  ®     0  —   ±  F!  `  ê!    h&quot;  ‚  þ&quot;    w#  ‘  þ#    j$  „  ï$      ‚%  œ  &amp;     ®&amp;  ¤   M'  C  â'  Ø  ^(  T  )  ý  q)  g  ê)  à  f*  \  ý*  ó  +  w  ,  ø  b,  X  é,  ß  d-  Z  Ã-  ¹  H.  &gt;  ß.  Õ  ^/  T     0  ý    Š0  €
    1  ü
    Œ1  ‚  2    º2  °  33  )
    ±3  §
    14  '  ¬4  ¢  &amp;5    ˜5  Ž  6  
    –6  Œ  47  *  ¢7  ˜  8    †8  |  9  
    ¸9  ®  :    ®:  ¤  (;    Ž;  „   &lt;  ö  ’&lt;  ˆ  ù&lt;  ï  n=  d  Ë=  Á  Q&gt;  G  µ&gt;  «  8?  .  ¸?  ®  %@     @  –  3A  )  µA  «  ,B  &quot;  ¯B  ¥  C    oC  e  àC  Ö  _D  U  þD  ô  ’E  ˆ  F  ù  F  a   %G  ÷   ‚G  T  ïG  Á  MH    ÍH  Ÿ  RI  $  ÚI  ¬  eJ  7  K  ×  ‘K  c  %L  ÷  ÙL  «  VM  (  íM  ¿  |N  N  ÿN  Ñ  rO  D   P  ×    „P  V
    ëP  ½
    oQ  A  ïQ  Á  vR  H  S  á  ’S  d
    
  T  Ü
    ¡T  s  U  ð  žU  p  V  ë  ‹V  ]  W  á  ŒW  ^  GX    ÒX  ¤  xY  J  Z  Ø  uZ  G  
  [  Ü  }[  O  \  ä  –\  h  .]     £]  u  C^    Ô^  ¦  J_    ì_  ¾  }`  O  ì`  ¾  ya  K  b  Ó  b  S  úb  Ì  ƒc  U  óc  Å  vd  H  üd  Î  …e  W     f  Û  Øf  †   Ag  ï   Êg  x  ih    õh  £  wi  %  äi  ’  Zj    Ïj  }  Ak  ï  ¾k  l  l  É  Žl  &lt;  m  ¼  ³m  a  !n  Ï  n  &gt;  o  À  œo  J     ÿo  ­   Žp  &lt;
    q  Å
  </code></pre>
  <p>Maybe this post is providing the solution, this has a similar problem. However I do not know how to put it in the code...
  <a href="https://stackoverflow.com/questions/50706561/c-sharp-files-downloaded-with-httpwebrequest-and-cookies-get-corrupted">c# files downloaded with httpwebrequest and cookies get corrupted</a></p>