栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > .Net

.NET物理路径,虚拟路径,物理路径与虚拟路径转换,文件下载方法

.Net 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

.NET物理路径,虚拟路径,物理路径与虚拟路径转换,文件下载方法

有时候报错:是物理路径,但应为虚拟路径。

1 什么是物理路径?什么是虚拟路径?
(1) 例子:
  用IIS举个例子:  
  WEB服务目录是d:路径  
  那么用HTTP访问网站根目录的时候,其实访问的是d:路径,那么其中虚拟路径就是(根),物理路径就是d:路径
 
(2) 我自己的理解:绝对路径一般都是带有磁盘完成路径, 而虚拟路径一般不带有磁盘

2 程序中虚拟路径和物理路径的转化

 

#region 物理路径和相对路径的转换
//本地路径转换成URL相对路径 
private string urlconvertor(string imagesurl1)
{
	string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录
	string imagesurl2 = imagesurl1.Replace(tmpRootDir, ""); //转换成相对路径
	imagesurl2 = imagesurl2.Replace(@"", @"/");
	//imagesurl2 = imagesurl2.Replace(@"Aspx_Uc/", @"");
	return imagesurl2;
}
//相对路径转换成服务器本地物理路径 
private string urlconvertorlocal(string imagesurl1)
{
	string tmpRootDir = Server.MapPath(System.Web.HttpContext.Current.Request.ApplicationPath.ToString());//获取程序根目录
	string imagesurl2 = tmpRootDir + imagesurl1.Replace(@"/", @""); //转换成绝对路径
	return imagesurl2;
}
#endregion

3 下载的方法

/// 
/// 获取物理地址
/// 
public static string MapPathFile(string FileName)
{
	return HttpContext.Current.Server.MapPath(FileName);
}
/// 
/// 普通下载
/// 
/// 文件虚拟路径
public static bool DownLoadold(string FileName)
{
	bool bools = false;
	string destFileName = MapPathFile(FileName);
	if (File.Exists(destFileName))
	{
		FileInfo fi = new FileInfo(destFileName);
		HttpContext.Current.Response.Clear();
		HttpContext.Current.Response.ClearHeaders();
		HttpContext.Current.Response.Buffer = false;
		HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(Path.GetFileName(destFileName), System.Text.Encoding.UTF8));
		HttpContext.Current.Response.AppendHeader("Content-Length", fi.Length.ToString());
		HttpContext.Current.Response.ContentType = "application/octet-stream";
		HttpContext.Current.Response.WriteFile(destFileName);
		HttpContext.Current.Response.Flush();
		HttpContext.Current.Response.End();
		bools = true;
	}
	return bools;
}

public static void ResponseFile(string path, HttpContext context)
{
	context = HttpContext.Current;

	System.IO.Stream iStream = null;
	byte[] buffer = new Byte[10000];
	int length;
	long dataToRead;
	string filename = System.IO.Path.GetFileName(path);

	try
	{
		iStream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
		dataToRead = iStream.Length;
		context.Response.ContentType = "application/octet-stream";
		context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(filename, System.Text.Encoding.UTF8));

		while (dataToRead > 0)
		{
			if (context.Response.IsClientConnected)
			{
				length = iStream.Read(buffer, 0, 10000);
				context.Response.OutputStream.Write(buffer, 0, length);
				context.Response.Flush();

				buffer = new Byte[10000];
				dataToRead = dataToRead - length;
			}
			else
			{
				dataToRead = -1;
			}
		}
	}
	catch (Exception ex)
	{
		context.Response.Write(ex.Message);
	}
	finally
	{
		if (iStream != null)
		{
			iStream.Close();
		}
	}
}

public static void ResponseFile(string path, string fileName, HttpContext context)
{
	context = HttpContext.Current;

	System.IO.Stream iStream = null;
	byte[] buffer = new Byte[10000];
	int length;
	long dataToRead;

	try
	{
		iStream = new System.IO.FileStream(path, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
		dataToRead = iStream.Length;
		context.Response.ContentType = "application/octet-stream";
		context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));

		while (dataToRead > 0)
		{
			if (context.Response.IsClientConnected)
			{
				length = iStream.Read(buffer, 0, 10000);
				context.Response.OutputStream.Write(buffer, 0, length);
				context.Response.Flush();

				buffer = new Byte[10000];
				dataToRead = dataToRead - length;
			}
			else
			{
				dataToRead = -1;
			}
		}
	}
	catch (Exception ex)
	{
		context.Response.Write(ex.Message);
	}
	finally
	{
		if (iStream != null)
		{
			iStream.Close();
		}
	}
}

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/906062.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号