Hi nedash,
Please refer the below code that full-fill your requirement.
HTML
<div>
<asp:DropDownList ID="ddlDonwloadSpeed" runat="server">
<asp:ListItem Value="20">20 Kb/s</asp:ListItem>
<asp:ListItem Value="50">50 Kb/s</asp:ListItem>
</asp:DropDownList>
<asp:Button ID="btnDownload" runat="server" Text="Download" OnClick="btnDownload_Click" />
</div>
Code
C#
protected void btnDownload_Click(object sender, EventArgs e)
{
string outputFileName = "Wildlife.wmv";
string filePath = Server.MapPath("~/Video/Wildlife.wmv");
string value = ddlDonwloadSpeed.SelectedValue;
int downloadSpeed = 1024 * int.Parse(value);
Response.Clear();
try
{
DownloadFileWithLimitedSpeed(outputFileName, filePath, downloadSpeed);
}
catch (Exception ex)
{
Response.Write(@"<p><font color=""red"">");
Response.Write(ex.Message);
Response.Write(@"</font></p>");
}
Response.End();
}
public void DownloadFileWithLimitedSpeed(string fileName, string filePath, long downloadSpeed)
{
if (!File.Exists(filePath))
{
throw new Exception("Err: There is no such a file to download.");
}
using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
using (BinaryReader br = new BinaryReader(fs))
{
Response.Buffer = false;
long fileLength = fs.Length;
int pack = 1024;
int sleep = (int)Math.Ceiling(1000.0 * pack / downloadSpeed);
Response.AddHeader("Content-Length", fileLength.ToString());
Response.ContentType = "application/octet-stream";
string utf8EncodingFileName = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8);
Response.AddHeader("Content-Disposition", "attachment;filename=" + utf8EncodingFileName);
int maxCount = (int)Math.Ceiling(Convert.ToDouble(fileLength) / pack);
for (int i = 0; i < maxCount; i++)
{
if (Response.IsClientConnected)
{
Response.BinaryWrite(br.ReadBytes(pack));
Thread.Sleep(sleep);
}
else
{
break;
}
}
}
}
}
VB .Net
Protected Sub btnDownload_Click(sender As Object, e As EventArgs)
Dim outputFileName As String = "Wildlife.wmv"
Dim filePath As String = Server.MapPath("~/Video/Wildlife.wmv")
Dim value As String = ddlDonwloadSpeed.SelectedValue
Dim downloadSpeed As Integer = 1024 * Integer.Parse(value)
Response.Clear()
Try
DownloadFileWithLimitedSpeed(outputFileName, filePath, downloadSpeed)
Catch ex As Exception
Response.Write("<p><font color=""red"">")
Response.Write(ex.Message)
Response.Write("</font></p>")
End Try
Response.[End]()
End Sub
Public Sub DownloadFileWithLimitedSpeed(fileName As String, filePath As String, downloadSpeed As Long)
If Not File.Exists(filePath) Then
Throw New Exception("Err: There is no such a file to download.")
End If
Using fs As New FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite)
Using br As New BinaryReader(fs)
Response.Buffer = False
Dim fileLength As Long = fs.Length
Dim pack As Integer = 1024
Dim sleep As Integer = CInt(Math.Ceiling(1000.0 * pack / downloadSpeed))
Response.AddHeader("Content-Length", fileLength.ToString())
Response.ContentType = "application/octet-stream"
Dim utf8EncodingFileName As String = HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8)
Response.AddHeader("Content-Disposition", Convert.ToString("attachment;filename=") & utf8EncodingFileName)
Dim maxCount As Integer = CInt(Math.Ceiling(Convert.ToDouble(fileLength) / pack))
For i As Integer = 0 To maxCount - 1
If Response.IsClientConnected Then
Response.BinaryWrite(br.ReadBytes(pack))
Thread.Sleep(sleep)
Else
Exit For
End If
Next
End Using
End Using
End Sub
Screenshot