title: Uri encoding, file download doesn't work with , and whitespace
body: <p>I have an asp.net webpage that allows downloading files.</p>
  
  <p>When i had the problem of downloading a file with <code>whitespace(Test 1 4 3.txt)</code> it will turn the file into: <code>Test+1+4+3.txt</code></p>
  
  <p>I used:</p>
  
  <pre><code>_Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8));
  </code></pre>
  
  <p>And it solved the issue.</p>
  
  <p>Now i have a new issue:
  When a file contains <code>,</code> it changes it into <code>%2c</code> and i the UrlEncode doesn't fix it.
  i Tried using:</p>
  
  <pre><code>_Response.AddHeader("Content-Disposition", "attachment;filename=" + Uri.EscapeDataString(_fileName));
  </code></pre>
  
  <p>But it's the old setting and it doesn't support whitespace.</p>
  
  <p>What can i do to solve such a case?
  Should i use regex / switch?</p>
  
  <p>This is my function:</p>
  
  <pre><code> public static bool ResponseFile(HttpRequest _Request, HttpResponse _Response, string _fileName, string _fullPath, long _speed = 1024000)
      {
          try
          {
              FileStream myFile = new FileStream(_fullPath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
              BinaryReader br = new BinaryReader(myFile);
              try
              {
                  _Response.AddHeader("Accept-Ranges", "bytes");
                  _Response.Buffer = false;
                  long fileLength = myFile.Length;
                  long startBytes = 0;
  
                  int pack = 10240; //10K bytes
                  int sleep = (int)Math.Floor((double)(1000 * pack / _speed)) + 1;
                  if (_Request.Headers["Range"] != null)
                  {
                      _Response.StatusCode = 206;
                      string[] range = _Request.Headers["Range"].Split(new char[] { '=', '-' });
                      startBytes = Convert.ToInt64(range[1]);
                  }
                  _Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
                  if (startBytes != 0)
                  {
                      _Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
                  }
                  _Response.AddHeader("Connection", "Keep-Alive");
                  _Response.ContentType = "application/octet-stream";
  
                  //Old didn't work with both + and ,
                  //_Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(_fileName, System.Text.Encoding.UTF8));
                  _Response.AddHeader("Content-Disposition", "attachment;filename=" + Uri.EscapeDataString(_fileName));
  
                  br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
                  int maxCount = (int)Math.Floor((double)((fileLength - startBytes) / pack)) + 1;
  
                  for (int i = 0; i &lt; maxCount; i++)
                  {
                      if (_Response.IsClientConnected)
                      {
                          _Response.BinaryWrite(br.ReadBytes(pack));
                          Thread.Sleep(sleep);
                      }
                      else
                      {
                          i = maxCount;
                      }
                  }
              }
              catch
              {
                  return false;
              }
              finally
              {
                  br.Close();
                  myFile.Close();
              }
          }
          catch
          {
              return false;
          }
          return true;
      }
  </code></pre>