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:
Here is an example of IIS:
The Web service directory is D: \ path
When using HTTP to access the root directory of the website, the actually accessed path is D: \, where the virtual path is \ (Root), and the physical path is D: \.
 
(2) my own understanding: absolute paths generally carry disk completion paths, while virtual paths generally do not have Disks

2 conversion of virtual paths and physical paths in the program

# Region physical path and relative path Conversion
// Convert the local path to the relative URL path
Private string urlconvertor (string imagesurl1)
{
String tmprootdir = server. mappath (system. Web. httpcontext. Current. Request. applicationpath. tostring (); // obtain the program root directory
String imagesurl2 = imagesurl1.replace (tmprootdir, ""); // convert to relative path
Imagesurl2 = imagesurl2.replace (@"\",@"/");
// Imagesurl2 = imagesurl2.replace (@ "aspx_uc /",@"");
Return imagesurl2;
}
// Convert the relative path to the local physical path of the server
Private string urlconvertorlocal (string imagesurl1)
{
String tmprootdir = server. mappath (system. Web. httpcontext. Current. Request. applicationpath. tostring (); // obtain the program root directory
String imagesurl2 = tmprootdir + imagesurl1.replace (@ "/", @ "\"); // convert to absolute path
Return imagesurl2;
}
# Endregion

3. Download Method

/// <Summary>
/// Obtain the physical address
/// </Summary>
Public static string mappathfile (string filename)
{
Return httpcontext. Current. server. mappath (filename );
}
/// <Summary>
/// Normal download
/// </Summary>
/// <Param name = "FILENAME"> Virtual File 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 ();
}
}
}

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.