ASP and C # how to hide the File Download path

Source: Internet
Author: User
Tags dname
The download path is hidden by passing the ID in the database, and then reading the path in the database through the passed ID on the other page.

  1. The Implementation Method in C # (only the. aspx. CS code is required)

String sel_ SQL = "select filevisualpath from tabmailattachfiles where fileid =" + request. querystring ["destfilename"]. tostring ();

// String destfilename = request. querystring ["destfilename"]! = NULL? Request. querystring ["destfilename"]: "";
String destfilename = Roa. components. fune_performance.execad (sel_ SQL). Tables [0]. Rows [0] [0]. tostring ();
Destfilename = server. mappath (".") + destfilename;
Destfilename = server. urldecode (destfilename );
If (file. exists (destfilename ))
{
Fileinfo Fi = new fileinfo (destfilename );
Response. Clear ();
Response. clearheaders ();
Response. Buffer = false;

// Response. appendheader ("content-disposition", "attachment; filename =" + httputility. urlencode (path. getfilename (destfilename), system. Text. encoding. Default ));
Response. appendheader ("content-disposition", "attachment; filename =" + httputility. urlencode (path. getfilename (destfilename), system. Text. encoding. utf8 ));
Response. appendheader ("Content-Length", Fi. length. tostring ());
Response. contenttype = "application/octet-stream ";
Response. writefile (destfilename );
Response. Flush ();
Response. End ();
}
Else
{
Response. Write ("<SCRIPT langauge = JavaScript> alert ('the file does not exist! '); History. Go (-1); </SCRIPT> ");
Response. End ();
}

  2. Implementation in ASP

<! -- # Include file = "conn. asp" -->
<%
Response. Buffer = true
Response. Clear
Dim URL
Dim FSO, FL, flsize
Dim dname
Dim objstream, contenttype, flname, isre, url1
'*************************************** * ***** The downloaded file name passed in during the call
M_id = trim (request. querystring ("ID "))
Set rs = server. Createobject ("ADODB. recordset ")
SQL = "select * From dataview where xsf = true and ID =" & m_id
Rs. Open SQL, Conn, 1, 1
If not Rs. EOF then
Dname = RS ("path ")
Else
Response. Write ("No found ")
Response. End
End if
Rs. Close: Set rs = nothing
'*************************************** ******
If dname <> "then
* Download the server directory where the file is stored.
Url = server. mappath (dname)
Else
Response. Write ("No found ")
Response. End
End if
Set FSO = server. Createobject ("scripting. FileSystemObject ")
Set FL = FSO. GetFile (URL)
Flsize = fL. Size
Flname = fL. Name
Set FL = nothing
Set FSO = nothing
Set objstream = server. Createobject ("ADODB. Stream ")
Objstream. Open
Objstream. type = 1
Objstream. loadfromfile URL
Select case lcase (right (flname, 4 ))
Case ". ASF"
Contenttype = "Video/X-MS-ASF"
Case ". Avi"
Contenttype = "Video/Avi"
Case ". Doc"
Contenttype = "application/MSWord"
Case ". Zip"
Contenttype = "application/zip"
Case ". xls"
Contenttype = "application/vnd. MS-excel"
Case ". GIF"
Contenttype = "image/GIF"
Case ". jpg", "Jpeg"
Contenttype = "image/JPEG"
Case ". BMP"
Contenttype = "image/BMP"
Case ". ppt"
Contenttype = "application/vnd. MS-PowerPoint"
Case ". mdb"
Contenttype = "application/X-msaccess"
Case ". wav"
Contenttype = "audio/WAV"
Case ". MP3"
Contenttype = "audio/mpeg3"
Case ". mpg", "MPEG"
Contenttype = "Video/MPEG"
Case ". rtf"
Contenttype = "application/rtf"
Case ". htm", "html"
Contenttype = "text/html"
Case ". txt"
Contenttype = "text/plain"
Case else
Contenttype = "application/octet-stream"
End select
Response. addheader "content-disposition", "attachment; filename =" & flname
Response. addheader "Content-Length", flsize
Response. charset = "UTF-8"
Response. contenttype = contenttype
Response. binarywrite objstream. Read
Response. Flush
Response. Clear ()
Objstream. Close
Set objstream = nothing
%>

 

Related Article

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.