Search code examples
c#asp.netfile-transferwindows-server-2012

Simulate file download over long distance


I have the following function block that complete fine when the client is accessing the server somewhere close like within the US. However, when the client is farther away, like Eastern Europe, the download of large files fails. Any ideas on how I can simulate traffic from a distant IP address? Or any other ideas to make this download and occur faster.

The files are moderatley large, i.e. 100 MB. They are on the same server as the web site is served from. I'm using C# .NET Framework 4.5 IIS 8

System.IO.Stream stream = null;
int bytesToRead = 262144;
byte[] buffer = new Byte[bytesToRead];

try
{
    var response = HttpContext.Current.Response;
    response.ClearContent();
    response.Clear();

    //get file from file system
    string path = Properties.Settings.Default.DownloadDirectory + fileId;
    var fileInfo = new FileInfo(path);
    if (fileInfo.Exists)
    {
        using (stream = new FileStream(path, FileMode.Open))
        {
            response.ContentType = mimeType;

            response.AddHeader("Content-Disposition", "attachment; filename=\"" + name + "\"");
            response.AddHeader("Content-Length", fileInfo.Length.ToString());

            int length;
            do
            {
                if (response.IsClientConnected)
                {
                    length = stream.Read(buffer, 0, bytesToRead);
                    response.OutputStream.Write(buffer, 0, length);
                    response.Flush();
                    buffer = new Byte[bytesToRead];
                }
                else
                {
                    length = -1;
                }
            } while (length > 0);

            stream.Close();
            response.End();
        }
    }
}

Solution

  • We ended up using HttpResponse.TransmitFile. It writes directly to the response output stream with out buffering into memory. Then we tested it using a proxies in different countries around the world.