Physical path, virtual path, physical path and virtual path conversion, File download method

Source: Internet
Author: User

1 What is a physical path? What is a virtual path?
(1) Example:
Use IIS for an example:
Web Service directory is a d:\ path
Then when using HTTP to access the root directory of the site, in fact, access to the D:\ path, then the virtual path is \ (root), the physical path is the d:\ path

(2) My own understanding: the absolute path is usually with the disk completion path, and the virtual path is generally not with the disk

2 conversion of virtual paths and physical paths in programs

#region transformation of physical and relative paths
Local path converted to URL relative path
private string Urlconvertor (String imagesurl1)
{
String tmprootdir = Server.MapPath (System.Web.HttpContext.Current.Request.ApplicationPath.ToString ());//Get Program root directory
String imagesurl2 = Imagesurl1. Replace (Tmprootdir, ""); Convert to relative path
Imagesurl2 = Imagesurl2. Replace (@ "\", @ "/");
Imagesurl2 = Imagesurl2. Replace (@ "aspx_uc/", @ "");
return IMAGESURL2;
}
Relative paths are converted to server local physical paths
private string Urlconvertorlocal (String imagesurl1)
{
String tmprootdir = Server.MapPath (System.Web.HttpContext.Current.Request.ApplicationPath.ToString ());//Get Program root directory
String imagesurl2 = Tmprootdir + IMAGESURL1. Replace (@ "/", @ "\"); Convert to Absolute path
return IMAGESURL2;
}
#endregion

3 Ways to download

<summary>
Get Physical Address
</summary>
public static string Mappathfile (String FileName)
{
Return HttpContext.Current.Server.MapPath (FileName);
}
<summary>
Normal download
</summary>
<param name= "filename" > File virtual path </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 ();
}
}
}

Physical path, virtual path, physical path and virtual path conversion, File download method

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.