概述
/**/
/// <summary>
/// 下载文件
/// </summary>
/// <param name= "filepath "> </param>
public static void DownloadFile( string filepath)
{
string strContentType = " ";
string strFileName = " ";
string strFilePath = " ";
int intIndex1 = -1;
int intIndex2 = -1;
int intIndex = 0;
if (filepath.EndsWith( ".doc "))
{
strContentType = "application/msword ";
}
else if (filepath.EndsWith( ".xls "))
{
//strContentType = "application/x-zaurus-xls "
strContentType = "application/vnd.ms-excel ";
}
else if (filepath.EndsWith( ".pdf "))
{
strContentType = "application/pdf ";
}
else if (filepath.EndsWith( ".ppt "))
{
//strContentType = "application/powerpoint "
strContentType = "application/vnd.ms-powerpoint ";
}
else if (filepath.EndsWith( ".jpg ") ¦ ¦ filepath.EndsWith( ".jpeg ") ¦ ¦ filepath.EndsWith( ".jpe "))
{
strContentType = "image/jpeg ";
}
else if (filepath.EndsWith( ".bmp "))
{
strContentType = "image/bmp ";
}
else if (filepath.EndsWith( ".gif "))
{
strContentType = "image/gif ";
}
else if (filepath.EndsWith( ".zip "))
{
strContentType = "application/zip ";
}
else if (filepath.EndsWith( ".txt "))
{
strContentType = "text/plain ";
}
else
{
strContentType = "application/octet-stream ";
}
intIndex1 = filepath.LastIndexOf( "/ ");
intIndex2 = filepath.LastIndexOf( "\ ");
if (intIndex1 > intIndex2)
{
intIndex = intIndex1;
}
else
{
intIndex = intIndex2;
}
if (intIndex > -1)
{
if (intIndex + 1 < filepath.Length)
strFileName = filepath.Substring(intIndex + 1);
}
else
{
strFileName = filepath;
}
// ファイルパス処理
if (filepath.StartsWith( "/ ") ¦ ¦ filepath.StartsWith( "\ "))
strFilePath = HttpContext.Current.Server.MapPath( "/ ") + filepath;
else
strFilePath = filepath;
if (FileUtil.isFileExist(strFilePath))
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = strContentType;
HttpContext.Current.Response.AddHeader( "Content-Disposition ", "attachment; filename= " + HttpUtility.UrlEncode(strFileName));
HttpContext.Current.Response.WriteFile(strFilePath);
//HttpContext.Current.Response.BinaryWrite(objFileIO.IFileByte(strFilePath));
//HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
}
else
{
SetGlobalMessageID(FindKeyValueCutNull(GXML.COMMONMESSAGE, GXML.DOWNLOAD_FILE_NOEXIST));
}
/// 下载文件
/// </summary>
/// <param name= "filepath "> </param>
public static void DownloadFile( string filepath)
{
string strContentType = " ";
string strFileName = " ";
string strFilePath = " ";
int intIndex1 = -1;
int intIndex2 = -1;
int intIndex = 0;
if (filepath.EndsWith( ".doc "))
{
strContentType = "application/msword ";
}
else if (filepath.EndsWith( ".xls "))
{
//strContentType = "application/x-zaurus-xls "
strContentType = "application/vnd.ms-excel ";
}
else if (filepath.EndsWith( ".pdf "))
{
strContentType = "application/pdf ";
}
else if (filepath.EndsWith( ".ppt "))
{
//strContentType = "application/powerpoint "
strContentType = "application/vnd.ms-powerpoint ";
}
else if (filepath.EndsWith( ".jpg ") ¦ ¦ filepath.EndsWith( ".jpeg ") ¦ ¦ filepath.EndsWith( ".jpe "))
{
strContentType = "image/jpeg ";
}
else if (filepath.EndsWith( ".bmp "))
{
strContentType = "image/bmp ";
}
else if (filepath.EndsWith( ".gif "))
{
strContentType = "image/gif ";
}
else if (filepath.EndsWith( ".zip "))
{
strContentType = "application/zip ";
}
else if (filepath.EndsWith( ".txt "))
{
strContentType = "text/plain ";
}
else
{
strContentType = "application/octet-stream ";
}
intIndex1 = filepath.LastIndexOf( "/ ");
intIndex2 = filepath.LastIndexOf( "\ ");
if (intIndex1 > intIndex2)
{
intIndex = intIndex1;
}
else
{
intIndex = intIndex2;
}
if (intIndex > -1)
{
if (intIndex + 1 < filepath.Length)
strFileName = filepath.Substring(intIndex + 1);
}
else
{
strFileName = filepath;
}
// ファイルパス処理
if (filepath.StartsWith( "/ ") ¦ ¦ filepath.StartsWith( "\ "))
strFilePath = HttpContext.Current.Server.MapPath( "/ ") + filepath;
else
strFilePath = filepath;
if (FileUtil.isFileExist(strFilePath))
{
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.ContentType = strContentType;
HttpContext.Current.Response.AddHeader( "Content-Disposition ", "attachment; filename= " + HttpUtility.UrlEncode(strFileName));
HttpContext.Current.Response.WriteFile(strFilePath);
//HttpContext.Current.Response.BinaryWrite(objFileIO.IFileByte(strFilePath));
//HttpContext.Current.Response.End();
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.Close();
}
else
{
SetGlobalMessageID(FindKeyValueCutNull(GXML.COMMONMESSAGE, GXML.DOWNLOAD_FILE_NOEXIST));
}
转载于:https://www.cnblogs.com/Nina-piaoye/archive/2007/11/13/958253.html
最后
以上就是矮小钢笔为你收集整理的下載文件的全部内容,希望文章能够帮你解决下載文件所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复