//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();
}
}