(原創)檔案安全下載

來源:互聯網
上載者:User

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;
            }
        }
    }
}

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.