下载的demo(可显示文件的总大小,并解决了下载完成自动结束下载)
//分块下载
private void downLoadPackage()
{
System.Web.HttpServerUtility server = System.Web.HttpContext.Current.Server;
string fileName = "aaa.apk";
//路径
string filePath = server.MapPath("DownLoadPackage/aaa.apk");
if (!File.Exists(filePath)) return;
FileInfo info = new FileInfo(filePath);
//指定块大小
long chunkSize = 4096;
//建立一个4K的缓冲区
byte[] buffer = new byte[chunkSize];
//剩余的字节数
long dataToRead = 0;
FileStream stream = null;
try
{
//打开文件
stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
dataToRead = stream.Length;
//添加Http头
HttpContext.Current.Response.ContentType = "application/octet-stream";
HttpContext.Current.Response.AddHeader("Content-Disposition", "attachement;filename=" + HttpUtility.UrlEncode(fileName));
HttpContext.Current.Response.AddHeader("Content-Length", dataToRead.ToString());
while (dataToRead > 0)
{
if (HttpContext.Current.Response.IsClientConnected)
{
int length = stream.Read(buffer, 0, Convert.ToInt32(chunkSize));
HttpContext.Current.Response.OutputStream.Write(buffer, 0, length);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Clear();
dataToRead -= length;
}
else
{
//防止client失去连接
dataToRead = -1;
}
}
}
catch (Exception ex)
{
HttpContext.Current.Response.Write("Error:" + ex.Message);
}
finally
{
if (stream != null)
{
stream.Close();
}
HttpContext.Current.Response.Close();
}
}
iis上无法下载.apk和.ipa的问题
单击“新建”,建立新的MIME类型;
扩展名是:.apk MIMI类型是:application/vnd.android.package-archive
扩展名是:.ipa MIMI类型是:application/iphone