物理路径,虚拟路径,物理路径与虚拟路径转换,文件下载方法
2017-05-03 16:22
405 查看
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 下载的方法
/// <summary>
/// 获取物理地址
/// </summary>
public static string MapPathFile(string FileName)
{
return HttpContext.Current.Server.MapPath(FileName);
}
/// <summary>
/// 普通下载
/// </summary>
/// <param name="FileName">文件虚拟路径</param>
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();
}
}
}
(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 下载的方法
/// <summary>
/// 获取物理地址
/// </summary>
public static string MapPathFile(string FileName)
{
return HttpContext.Current.Server.MapPath(FileName);
}
/// <summary>
/// 普通下载
/// </summary>
/// <param name="FileName">文件虚拟路径</param>
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();
}
}
}
相关文章推荐
- 物理路径,虚拟路径,物理路径与虚拟路径转换,文件下载方法
- 物理路径,虚拟路径,物理路径与虚拟路径转换,文件下载方法
- ASP.NET(5):虚拟路径转换到物理路径的一种实现方法,不用MapPath
- asp.net(c#)中相对路径(虚拟路径)和物理磁盘路径的转换
- ASP和C#隐藏文件下载路径的方法
- 转:100元寻找CMD命令下载http文件的方法,可以提价,非转换vbs的方案作品
- ASP和C#隐藏文件下载路径的方法
- 简单方式下载网页中的文件存储在本地物理路径,或转成字节数组存入数据库中
- 多线程之线程间的通信——以及下载文件并保存到指定路径的方法
- j2ee中下载文件,js调用action,及获得tomcat的物理路径等知识点
- 网站文件的绝对路径转换成相对路径方法
- ASP和C#隐藏文件下载路径的方法
- C#隐藏文件下载路径的方法
- 如何简单的下载一些小文件并保存在一个路径中(有些方法进行了封装)
- 定时发布任务,在global.asax中获取文件的物理路径的方法
- [置顶] asp.net(c#)中相对路径(虚拟路径)和物理磁盘路径的转换
- HttpRequest 对象 Request获取虚拟路径和物理路径的属性或方法
- asp.net读取服务器物理路径下载文件
- 获取IIS虚拟目录的物理路径的方法
- asp.net 获取项目(网站)的物理、虚拟路径的方法