1、首先打開Internet信息服務(IIS)管理器,選擇新建網站,如果沒有Internet信息服務(IIS)管理器,可以在控制面板添加,按照 控制面板\程序\程序和功能,點擊 打開或關閉Windows功能,添加 Internet信息,全部勾選,點擊 確定,等待安裝完成,之后再打開就有Internet信息服務,操作如下
2、添加網站,選擇 網站,右鍵 添加網站,彈出添加網站窗體,填寫網站信息 如:測試http網站部署文件服務器
3、切換到 功能視圖,雙擊 目錄瀏覽 ,右側區域,點擊 啟用。
4、同樣 切換到 功能視圖,雙擊 WebDAV創作規則,右側區域 選擇 添加創作規則
選擇 所有用戶,權限下的讀取,源,寫入全部勾選,點擊確定。
選擇 右側區域 WebDAV設置,設置請求篩選行為和屬性行為,點擊 右側 應用
在選擇網站測試http網站部署文件服務器,點擊功能視圖,右側區域 點擊 啟用WebDAV
5、同樣 切換到功視圖,雙擊 身份驗證,啟用 匿名身份驗證和Windows身份驗證
6、選擇網站 測試http網站部署文件服務器,右鍵 管理網站 瀏覽 ,瀏覽器顯示web.config文件,如下圖,即使成功。
7、添加用戶和設置網站物理路徑D:\TestHttpDeployFileServer下的文件夾的訪問權限TestHttpDeployFileServer
添加用戶:選擇桌面上的 我的電腦或計算機,右鍵 管理,選擇 本地用戶組下用戶,右側空白位置 右鍵 新用戶,
雙擊 添加的myjj用戶,設置 用戶 隸屬於 Power Users
設置網站物理路徑D:\TestHttpDeployFileServer下的文件夾的訪問權限TestHttpDeployFileServer:
選擇文件夾TestHttpDeployFileServer,右鍵屬性,選擇 安全 設置myjj用戶所屬用戶組的權限即可,如果沒有 Power Users用戶組,就選擇 添加 高級 立即查找,然后選擇 Power Users點擊確定,即可設置訪問權限
8、經過上述步驟,一個網站的http訪問的文件服務器已經架設成功了,下面就可以使用了,具體如下
新建控制台項目程序,Program.cs內容如下:
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
TestWebclient testWebclient = new TestWebclient();
testWebclient.WebClientUpload();//上傳
testWebclient.WebClientDownload();//下載
testWebclient.WebClientdelete();//刪除
//WebClientDownload();//下載
//WebClientUpload();//上傳
//WebClientDelete();//刪除
Console.ReadKey();
}
#region 下載
/// <summary>
/// 下載
/// </summary>
static void WebClientDownload()
{
WebClient webClient = new WebClient
{
Credentials = CredentialCache.DefaultCredentials
};
//Uri _uri = new Uri(@"http://localhost:8082/123.txt");
Uri uri = new Uri(@"http://192.168.0.100:8082/123.txt");
webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
webClient.DownloadFileCompleted += WebClient_DownloadFileCompleted;
webClient.DownloadFileAsync(uri, @"D:\download\123.txt");
}
private static void WebClient_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
Console.WriteLine("下載完成...");
}
private static void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Console.WriteLine($"{e.ProgressPercentage}:{e.BytesReceived}/{e.TotalBytesToReceive}");
}
#endregion
#region 上傳
/// <summary>
/// 上傳
/// </summary>
static void WebClientUpload()
{
WebClient webClient = new WebClient
{
Credentials = new NetworkCredential("test", "123")
};
Uri uri = new Uri(@"http://192.168.0.100:8082/456.xlsx");
webClient.UploadProgressChanged += WebClient_UploadProgressChanged;
webClient.UploadFileCompleted += WebClient_UploadFileCompleted;
webClient.UploadFileAsync(uri, "PUT", @"D:\download\456.xlsx");
}
private static void WebClient_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
{
Console.WriteLine("上傳完成...");
}
private static void WebClient_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
Console.WriteLine($"{e.ProgressPercentage}:{e.BytesSent}/{e.TotalBytesToSend}");
}
#endregion
#region 刪除
/// <summary>
/// 刪除
/// </summary>
static void WebClientDelete()
{
WebClient webClient = new WebClient
{
Credentials = new NetworkCredential("test", "123")
};
Uri uri = new Uri(@"http://192.168.0.100:8082/456.xlsx");
webClient.UploadDataCompleted += WebClient_UploadDataCompleted;
webClient.UploadDataAsync(uri, "DELETE", new byte[0]);
}
private static void WebClient_UploadDataCompleted(object sender, UploadDataCompletedEventArgs e)
{
Console.WriteLine("已刪除...");
}
#endregion
}
}
TestWebclient類代碼如下:
using System;
using System.Collections.Generic;
using System.Net;
using System.Text;
namespace ConsoleApp1
{
public class TestWebclient
{
#region 下載
public void WebClientDownload()
{
WebClient webClient = new WebClient();
webClient.Credentials = CredentialCache.DefaultCredentials;
Uri uri = new Uri(@"http://localhost:8087/123.txt");
webClient.DownloadProgressChanged += WebClient_DownloadProgressChanged;
webClient.DownloadDataCompleted += WebClient_DownloadDataCompleted;
webClient.DownloadFileAsync(uri, @"D:\download\123.txt");
}
private static void WebClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
Console.WriteLine($"已下載百分比:{e.ProgressPercentage}:{e.BytesReceived / e.TotalBytesToReceive}");
}
private static void WebClient_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e)
{
Console.WriteLine("下載完成...");
}
#endregion
#region 上傳
public void WebClientUpload()
{
WebClient webClient = new WebClient();
//webClient.Credentials = new NetworkCredential("test123","123");
webClient.Credentials = CredentialCache.DefaultCredentials;
Uri uri = new Uri(@"http://localhost:8087/456.xlsx");
webClient.UploadProgressChanged += WebClient_UploadProgressChanged;
webClient.UploadFileCompleted += WebClient_UploadFileCompleted;
webClient.UploadFileAsync(uri, "put", @"D:\download\456.xlsx");
}
private void WebClient_UploadFileCompleted(object sender, UploadFileCompletedEventArgs e)
{
Console.WriteLine("上傳完成");
}
private void WebClient_UploadProgressChanged(object sender, UploadProgressChangedEventArgs e)
{
Console.WriteLine($"{e.ProgressPercentage}:{e.BytesSent/e.TotalBytesToSend}");
}
#endregion
#region 刪除
public void WebClientdelete()
{
WebClient webClient = new WebClient();
webClient.Credentials = new NetworkCredential("test123","123");
//webClient.Credentials = CredentialCache.DefaultCredentials;
Uri uri = new Uri(@"http://localhost:8087/456.xlsx");
webClient.UploadDataCompleted += WebClient_UploadDataCompleted;
webClient.UploadDataAsync(uri, "DELETE", new byte[0]);
}
private void WebClient_UploadDataCompleted(object sender, UploadDataCompletedEventArgs e)
{
Console.WriteLine("已刪除");
}
#endregion
}
}