如果防止直接输入路径下载文件
例如文件的下载路径为:http://www.123.com/download/test.rar
如何才能使下载或打开文件时只能通过正常的下载导航来操作,例如只能通过单击“下载”按钮下载,而不是直接输入下载URL。如果直接输入URL则提示“文件无法打开或下载”
谢谢!
------解决方案--------------------see
http://www.cncsk.com/Document/WebDev/ASP_NET/200607048967.htm
------解决方案--------------------1、構造復雜無意義目錄、文件名
2、使用流下載模式
------解决方案--------------------private void DownloadFile(string physicalFilePath)
{
FileStream stream = null;
try
{
stream = new FileStream(physicalFilePath, FileMode.Open, FileAccess.Read, FileShare.Read);
int bufSize = (int)stream.Length;
byte[] buf = new byte[bufSize];
int bytesRead = stream.Read(buf, 0, bufSize);
HttpContext.Current.Response.ContentType = "application/octet-stream ";
//attachment是以附件的形式下载,也可以改为online在线找开.
HttpContext.Current.Response.AppendHeader( "Content-Disposition ", "attachment;filename= " + HttpUtility.UrlEncode(System.IO.Path.GetFileName(physicalFilePath), System.Text.Encoding.UTF8));
HttpContext.Current.Response.OutputStream.Write(buf, 0, bytesRead);
HttpContext.Current.Response.End();
}
finally
{
stream.Close();
}
}