Securing Large Downloads Using C# and IIS 7

前端 未结 4 1183
[愿得一人]
[愿得一人] 2021-01-17 23:49

Here\'s the setup:

  • 1 web server running a C# app to which my users (stored in a MySQL database on said server) authenticate.

  • 1 file server

4条回答
  •  猫巷女王i
    2021-01-17 23:57

    You know what? The KB article is poo. Here is my official recommendation:

    public void StreamFile(string filePath)
    {
        string fileName = Path.GetFileName(filePath);
    
        using (var fStream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read))
        {
            var contentLength = fStream.Length;
    
            if (Request.UserAgent.Contains("MSIE"))
            {
                Response.AddHeader("Content-Transfer-Encoding", "binary");
            }
    
            Response.ContentType = "application/octet-stream";
            Response.AddHeader("Content-Length", contentLength.ToString());
    
            // Even though "Content-Disposition" should have an upper-case "d", as per http://www.ietf.org/rfc/rfc2183.txt
            // IE fails to recognize this if the "d" is upper-cased.
            Response.AddHeader("Content-disposition", "attachment; filename=" + fileName);
    
            var buffer = new byte[8192];
    
            while (Response.IsClientConnected)
            {
                var count = fStream.Read(buffer, 0, buffer.Length);
                if (count == 0)
                {
                    break;
                }
    
                Response.OutputStream.Write(buffer, 0, count);
                Response.Flush();
            }
        }
    
        Response.Close();
    }
    

提交回复
热议问题