//private string fileName= "file:///"+ @"C:\Users\Administrator\Desktop\LocationSystem.exe"; //本地路徑
private string fileName = "http://127.0.0.1:8013/bin/Exe/LocationSystem.exe"; //服務端路徑
上述服務端路徑下載文件時,會報錯,提示:
請求篩選模塊被配置爲拒絕包含hiddenSegment節的URL中的路徑。
解決方案:
1. 打開 C:\Windows\System32\inetsrv\config\applicationHost.config 刪除HiddenSegements中bin節點
2.找到IIS網站文件夾,把程序從bin目錄,移到其他目錄下。
private string fileName = "http://127.0.0.1:8013/bin/Exe/LocationSystem.exe"; //原路徑
private string fileName = "http://127.0.0.1:8013/Setup/LocationSystem.exe"; //新路徑
下面是下載測試代碼:
之前說下載路徑中不能有中文,但是測試時,發現中文也可以下載成功。
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using System.IO;
using UnityEngine.UI;
using UnityEditor;
using System;
public class FileDownLoad : MonoBehaviour {
// public AudioSource audioSource;
string urlPath;//資源網絡路徑
string file_SaveUrl;//資源保存路徑
public static FileDownLoad instance;
public Button btnObj;
private WWW downloadOperation;
private bool isDownLoadStart;
public InputField UrlInput;
public Slider DownloadSlider;
private void Awake()
{
instance = this;
}
private void Start()
{
btnObj.onClick.AddListener(play);
}
void Update()
{
if(isDownLoadStart)
{
//判斷異步對象並且異步對象沒有加載完畢,顯示進度
if (downloadOperation != null && !downloadOperation.isDone)
{
//Debug.Log(string.Format("下載進度:{0:F}%", downloadOperation.progress * 100.0));
if (DownloadSlider)
{
DownloadSlider.value = downloadOperation.progress;
}
}
}
}
//private string fileName= "file:///"+ @"C:\Users\Administrator\Desktop\LocationSystem.exe";
private string fileName = "http://127.0.0.1:8013/Setup/LocationSystem.exe";
public void play()
{
if(UrlInput != null&&!string.IsNullOrEmpty(UrlInput.text))
{
fileName = UrlInput.text;
}
string path = fileName;
if (string.IsNullOrEmpty(path))
{
Debug.Log("路徑輸入錯誤");
return;
}
urlPath = path;
//file_SaveUrl = @"C:\Users\Administrator\Desktop\LocationSystem.exe";
file_SaveUrl = Application.dataPath + @"\LocationSystem.exe";
Debug.Log("urlPath : " + urlPath);
FileInfo file = new FileInfo(file_SaveUrl); //每次都重新計算
byte[] bytes = new byte[1024]; //
if (File.Exists(file_SaveUrl))//本地存在,刪除重新下載
{
try
{
File.Delete(file_SaveUrl);
}catch(Exception e)
{
Debug.Log(e.ToString());
}
}
StartCoroutine(DownFile(urlPath, file_SaveUrl, file, bytes));
}
IEnumerator DownFile(string url,string file_SaveUrl, FileInfo file, byte[] bytes)
{
downloadOperation = new WWW(url);
isDownLoadStart = true;
SetSliderState(true);
yield return downloadOperation;
if (downloadOperation.error != null)
{
Debug.Log(downloadOperation.error);
isDownLoadStart = false;
SetSliderState(false);
yield break;
}
if (downloadOperation.isDone)
{
isDownLoadStart = false;
SetSliderState(false);
Debug.Log("下載完成,文件大小 : " + downloadOperation.bytes.Length);
bytes = downloadOperation.bytes;
CreatFile(bytes, file);
Debug.Log("文件創建完成...");
Application.OpenURL(file_SaveUrl);
}
}
private void SetSliderState(bool isOn)
{
if (DownloadSlider)
{
DownloadSlider.gameObject.SetActive(isOn);
}
}
private void OnDisable()
{
isDownLoadStart = false;
}
/// <summary>
/// 文件流創建文件
/// </summary>
/// <param name="bytes"></param>
void CreatFile(byte[] bytes, FileInfo file)
{
Stream stream;
stream = file.Create();
stream.Write(bytes, 0, bytes.Length);
stream.Close();
stream.Dispose();
}
}