62,173
社区成员
发帖
与我相关
我的任务
分享
protected void DownFile(int fileID, string strType)
{
string strFileFullName = null;
FileInfo file = new FileInfo(strFileFullName);
if (file.Exists)
{
string strContentType = GetContentType(file.Extension.ToLower());
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = strContentType;
Response.AddHeader("Content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.Name));
Response.WriteFile(strFileFullName);
Response.Flush();
Response.End();
}
}
/// <summary>
/// 根据文件扩展名返回ContentType
/// </summary>
/// <param name="strFileExtension">文件扩展名</param>
/// <returns></returns>
private string GetContentType(string strFileExtension)
{
string strType = "application/octet-stream";
if (strFileExtension.Length > 1)
{
switch (strFileExtension.Substring(1))
{
case "avi":
strType = "video/avi";
break;
case "bmp":
strType = "application/x-bmp";
break;
case "css":
strType = "text/css";
break;
case "dbf":
strType = "application/x-dbf";
break;
case "doc":
case "dot":
strType = "application/msword";
break;
case "dwg":
strType = "application/x-dwg";
break;
case "dxf":
strType = "image/vnd.dxf ";
break;
case "gif":
strType = "image/gif";
break;
case "hrf":
strType = "application/x-hrf";
break;
case "htc":
strType = "text/x-component";
break;
case "htm":
case "html":
case "jsp":
strType = "text/html";
break;
case "ico":
strType = "image/x-icon";
break;
case "img":
strType = "application/x-img";
break;
case "java":
strType = "java/*";
break;
case "jfif":
case "jpe":
case "jpeg":
case "jpg":
strType = "image/jpeg";
break;
case "js":
strType = "application/x-javascript";
break;
case "m3u":
strType = "audio/mpegurl";
break;
case "m4e":
strType = "video/mpeg4";
break;
case "mdb":
strType = "application/msaccess";
break;
case "mfp":
strType = "application/x-shockwave-flash";
break;
case "mid":
case "midi":
strType = "audio/mid";
break;
case "movie":
strType = "video/x-sgi-movie";
break;
case "mp3":
strType = "audio/mp3";
break;
case "mp4":
strType = "video/mpeg4";
break;
case "mpd":
strType = "application/vnd.ms-project";
break;
case "mpe":
strType = "video/x-mpeg";
break;
case "mpeg":
case "mpg":
strType = "video/mpg";
break;
case "pdf":
strType = "application/pdf";
break;
case "png":
strType = "image/png";
break;
case "ppt":
case "pps":
strType = "application/vnd.ms-powerpoint";
break;
case "rmvb":
strType = "application/vnd.rn-realmedia-vbr";
break;
case "rms":
strType = "application/vnd.rn-realmedia-secure";
break;
case "rm":
strType = "application/vnd.rn-realmedia";
break;
case "tif":
strType = "image/tiff";
break;
case "vss":
strType = "application/vnd.visio";
break;
case "wav":
strType = "audio/wav";
break;
case "wma":
strType = "audio/x-ms-wma";
break;
case "xls":
strType = "application/vnd.ms-excel";
break;
case "xml":
case "xsd":
case "tsd":
strType = "text/xml";
break;
case "swf":
strType = "application/x-shockwave-flash";
break;
case "txt":
strType = "text/plain";
break;
case "wmv":
strType = "audio/x-ms-wmv";
break;
case "wmx":
strType = "video/x-ms-wmx";
break;
}
}
return strType;
}
}
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
public partial class DownLoadAttFile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string url = System.Configuration.ConfigurationManager.AppSettings["VisualUrl"];
int access = 0;
if (Request["fileid"] != null && Request["type"] != null)
{
//附件ID提供2种形式解析,guid和int
bool isGuid = true;
if (Request["fileid"] != null && Request["type"] != null)
{
try
{
if (!isGuid)
{
int fid = 0;
int.TryParse(Request["fileid"], out fid);
if (fid > 0)
DownFile(fid, Request["type"]);
}
else
{
Guid gid = new Guid(Request["fileid"]);
DownFile(gid, Request["type"]);
}
}
catch
{
}
}
}
}
protected void DownFile(Guid fileID,string strType)
{
string strFileFullName = null;
//获取文件路径
FileInfo file = new FileInfo(strFileFullName);
if (file.Exists)
{
string strContentType = GetContentType(file.Extension.ToLower());
Response.Clear();
Response.ClearHeaders();
Response.ClearContent();
Response.ContentType = strContentType;
Response.AddHeader("Content-disposition", "attachment; filename=" + HttpUtility.UrlEncode(file.Name));
Response.WriteFile(strFileFullName);
Response.Flush();
Response.End();
}
else
Response.Redirect("~/Error/NoFind.aspx");
}
public static void Download(Page page, string FilePath)
{
string FileName = string.Empty;
string FileNameWithOutExt = System.IO.Path.GetFileNameWithoutExtension(FilePath);
string Ext = System.IO.Path.GetExtension(FilePath);
string[] name = FileNameWithOutExt.Split('^');
FileName = name[0] + Ext;
FileStream myFile = new FileStream(FilePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
BinaryReader br = new BinaryReader(myFile);
page.Response.AddHeader("Accept-Ranges", "bytes");
page.Response.Buffer = false;
long fileLength = myFile.Length;
long startBytes = 0;
int pack = 51200; //10K bytes
int sleep = 20; //每秒50次 即50*10K bytes每秒,
//int sleep = (int)Math.Floor(1000 * pack / _speed) + 1;
if (page.Request.Headers["Range"] != null)
{
page.Response.StatusCode = 206;
string[] range = page.Request.Headers["Range"].Split(new char[] { '=', '-' });
startBytes = Convert.ToInt64(range[1]);
}
page.Response.AddHeader("Content-Length", (fileLength - startBytes).ToString());
if (startBytes != 0)
{
page.Response.AddHeader("Content-Range", string.Format(" bytes {0}-{1}/{2}", startBytes, fileLength - 1, fileLength));
}
page.Response.AddHeader("Connection", "Keep-Alive");
page.Response.ContentType = "application/octet-stream";
page.Response.AddHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(FileName, System.Text.Encoding.UTF8));
br.BaseStream.Seek(startBytes, SeekOrigin.Begin);
int maxCount = (int)Math.Floor(Convert.ToDouble((fileLength - startBytes) / pack)) + 1;
for (int i = 0 ; i < maxCount ; i++)
{
if (page.Response.IsClientConnected)
{
page.Response.BinaryWrite(br.ReadBytes(pack));
Thread.Sleep(sleep);
}
else
{
i = maxCount;
}
}
br.Close();
myFile.Close();
}