zoukankan      html  css  js  c++  java
  • (原创)文件安全下载

    1、IIS设置

    1)启动IIS.

    2)创建一个新的名字为Security的虚拟目录.

      如图设置:

    3)创建一个名字为Download的虚拟目录

      设置如图:

      

    2、修改项中的web.config

    代码
    //下载目录相对路径
    <add key="UploadPath" value="/Security/Uploads" />

    //下载目录虚拟路径
    <add key="DownloadURL" value="http://localhost/downloads" />

    //windows用户名
    <add key="BasicAuthenticationUser" value="administrator" />

    //windows用户密码
    <add key="BasicAuthenticationPWD" value="admin$123" />

    3、下载程序

    代码
    using System;
    using System.Collections;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Net;
    using System.IO;
    namespace security
    {
        
    /// <SUMMARY>

        
    /// </SUMMARY>
        public class SecureFile
        {
            
    public SecureFile()
            {

            }
            
    public bool UploadFile(HtmlInputFile inputfile)
            {
                
    try
                {
                    
    string fileName = "";
                    
    string DirPath = "";
                
                    
    if(( inputfile.PostedFile != null ) && 
                       ( inputfile.PostedFile.ContentLength 
    > 0 ))
                    {
                        DirPath
    =HttpContext.Current.Server.MapPath(
                          System.Configuration.ConfigurationSettings.AppSettings[
                          
    "UploadPath"]);
                        fileName 
    = System.IO.Path.GetFileName(
                                         inputfile.PostedFile.FileName );
                        inputfile.PostedFile.SaveAs( DirPath 
    + "\\\" + fileName  );
                    }
                    
    return true;
                }
                
    catch
                {
                    
    return false;
                }
            }

            
    public bool DownloadFile(string strFile)
            {
                
    try
                {
                    
    string strDownloadURL=
                      System.Configuration.ConfigurationSettings.AppSettings[
                      
    "DownloadURL"];
                    
    string strUser=
                      System.Configuration.ConfigurationSettings.AppSettings[
                      
    "BasicAuthenticationUser"];
                    
    string strPWD=
                      System.Configuration.ConfigurationSettings.AppSettings[
                      
    "BasicAuthenticationPWD"];
                    
    string strURL=strDownloadURL + "\\\" + strFile;

                    WebClient req=new WebClient();

                    CredentialCache mycache=new CredentialCache();
                    mycache.Add(
    new Uri(strURL),"Basic"
                                
    new NetworkCredential(strUser,strPWD));
                    req.Credentials
    =mycache;
                    HttpResponse response = HttpContext.Current.Response;
                    response.Clear();
                    response.ClearContent();
                    response.ClearHeaders();
                    response.Buffer
    = true;

                    response.AddHeader("Content-Disposition"
                      
    "attachment;filename=\"" + strFile + "\"");
                    
    byte[] data=req.DownloadData(strURL);
                    response.BinaryWrite(data);
                    response.End();
                    
    return true;
                }
                
    catch(Exception ex)
                {
                    
    if(ex.Message=="The remote server " + 
                                   
    "returned an error: (404) Not Found.")
                        
    throw new Exception("File not found");
                    
    else if(ex.Message=="The remote server" + 
                            
    " returned an error: (401) Unauthorized.")
                        
    throw new Exception("Unauthorized access");

                    
    return false;
                }
            }
        }
    }
  • 相关阅读:
    [转]Could not load file or assembly 'XXX' or one of its dependencies.
    网页上显示别人电脑没安装的字体,例如LED字体
    JS 保留小数点后面2位小数
    ASP.NET2.0揭秘读书笔记五——维护应用程序状态之cookie
    C#高级编程读书笔记之.NET体系结构
    ASP.NET2.0揭秘读书笔记之八——页面输出缓存
    《大话设计模式》读书笔记一 简单工厂模式
    C#高级编程读书笔记之继承
    ASP.NET 2.0揭秘读书笔记七——使用用户配置文件Profile
    终于成功安装了SQL SqlServer2005
  • 原文地址:https://www.cnblogs.com/ghfsusan/p/1620344.html
Copyright © 2011-2022 走看看