ASP and C # methods to hide file download paths

Source: Internet
Author: User
Tags dname end flush tostring urlencode zip
Download

The shadowing of the download path is done by passing the ID in one database and then reading the path in the database by passing the ID on another page.

  1, C # Implementation of the method (only need. ASPX. CS code is OK)

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_commerce.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" (' file does not exist! '); History.go ( -1);</script> ");
Response.End ();
}

  2, in the ASP implementation method

<!--#include file= "conn.asp"-->
<%
Response.Buffer = True
Response.Clear
Dim url
Dim fso,fl,flsize
Dim dname
Dim OBJSTREAM,CONTENTTYPE,FLNAME,ISRE,URL1
' Download filename passed in when ********************************************* 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 files stored in the server directory
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
%>

Source: "The cloud Lost in the blue sky" BLOG



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.