1.打開對話框 要引用 System.Windows.Forms 注意 區(qū)分32 和64位2. System.Windows.Forms 放在Plugins文件夾下啤贩,同時需要更改.net 2.0 Subset為.net2.0?
代碼 :
using System;
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Windows.Forms;
using UnityEngine;
using UnityEngine.Networking;
public class DownloadFile : MonoBehaviour {
? ? // Use this for initialization
? ? void Start()
? ? {
? ? }
? ? IEnumerator GetPicData()
? ? {
? ? ? ? using (WWW www = new WWW("http://192.168.137.1:8001/pic/n.jpg"))
? ? ? ? {
? ? ? ? ? ? yield return www;
//獲取下載的文件名
string fileinfo=www.responseHeaders["Content-Disposition"];
string key="filename=";
string fileName=fileinfo.Substring(fileinfo.LastIndexOf(key).Replace(key,""));
? ? ? ? ? ? Stream inStream = new MemoryStream(www.bytes);//獲取http
? ? ? ? ? ? FileTool.SaveFileInfo saveFileInfo = new FileTool.SaveFileInfo();
? ? ? ? ? ? saveFileInfo.inStream = inStream;
? ? ? ? ? ? saveFileInfo.fileName =fileName;
? ? ? ? ? ? saveFileInfo.fileTittle = "保存圖片";
? ? ? ? ? ? saveFileInfo.fileFilter = FileTool.FileFilter.pic;
? ? ? ? ? ? FileTool.OpenDialog(saveFileInfo);
? ? ? ? }
? ? }
? ? IEnumerator GetExcelData()
? ? {
? ? ? ? using (WWW www = new WWW("http://192.168.137.1:8001/excel/new.xls"))
? ? ? ? {
? ? ? ? ? ? yield return www;? ? ? ? ?
//獲取下載的文件名
string fileinfo=www.responseHeaders["Content-Disposition"];
string key="filename=";
string fileName=fileinfo.Substring(fileinfo.LastIndexOf(key).Replace(key,""));
? ? ? ? ? ? Stream inStream = new MemoryStream(www.bytes);//獲取http
? ? ? ? ? ? FileTool.SaveFileInfo saveFileInfo = new FileTool.SaveFileInfo();
? ? ? ? ? ? saveFileInfo.inStream = inStream;
? ? ? ? ? ? saveFileInfo.fileName = fileName;
? ? ? ? ? ? saveFileInfo.fileTittle = "保存文件";
? ? ? ? ? ? saveFileInfo.fileFilter = FileTool.FileFilter.excel;
? ? ? ? ? ? FileTool.OpenDialog(saveFileInfo);
? ? ? ? }
? ? }
? ? // Update is called once per frame
? ? void Update () {
if (Input.GetKeyUp(KeyCode.B))
{
? ? ? ? ? ? StartCoroutine(GetExcelData());
? ? ? ? }
? ? ? ? if (Input.GetKeyUp(KeyCode.C))
? ? ? ? {
? ? ? ? ? ? StartCoroutine(GetPicData());
? ? ? ? }
? ? }
}
using System.Collections;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Windows.Forms;
using UnityEngine;
public class FileTool
{
? ? public static void OpenDialog(SaveFileInfo saveFileInfo)
? ? {
? ? ? ? using (SaveFileDialog saveFile = new SaveFileDialog())
? ? ? ? {
? ? ? ? ? ? saveFile.FileName = saveFileInfo.fileName;
? ? ? ? ? ? saveFile.Title = saveFileInfo.fileTittle;
? ? ? ? ? ? saveFile.Filter = saveFileInfo.fileFilter;
? ? ? ? ? ? saveFile.InitialDirectory = saveFileInfo.fileInitialDirectory;
? ? ? ? ? ? if (saveFile.ShowDialog() == DialogResult.OK)
? ? ? ? ? ? {
? ? ? ? ? ? ? ? using (Stream s = saveFile.OpenFile())
? ? ? ? ? ? ? ? {
? ? ? ? ? ? ? ? ? ? WriteFile(saveFileInfo.inStream, s);
? ? ? ? ? ? ? ? }
? ? ? ? ? ? ? ? string Savepath = Path.GetDirectoryName(saveFile.FileName);
? ? ? ? ? ? ? ? Process.Start(Savepath);
? ? ? ? ? ? }
? ? ? ? }
? ? }
? ? /// <summary>
? ? /// 將二進(jìn)制寫入文件
? ? /// </summary>
? ? /// <param name="inStream">輸入流</param>
? ? /// <param name="outStream">輸出流</param>
? ? public static void WriteFile(Stream inStream, Stream outStream)
? ? {
? ? ? ? byte[] b = new byte[1024];
? ? ? ? int readCount = inStream.Read(b, 0, b.Length);//讀流
? ? ? ? while (readCount > 0)
? ? ? ? {
? ? ? ? ? ? outStream.Write(b, 0, readCount);//寫流
? ? ? ? ? ? readCount = inStream.Read(b, 0, b.Length);//再讀流
? ? ? ? }
? ? ? ? outStream.Close();
? ? ? ? inStream.Close();
? ? }
? ? /// <summary>
? ? /// 打開對話框 保存文件 所填寫的信息
? ? /// </summary>
? ? public class SaveFileInfo
? ? {
? ? ? ? /// <summary>
? ? ? ? /// 需要保存的文件名
? ? ? ? /// </summary>
? ? ? ? public string fileName;
? ? ? ? /// <summary>
? ? ? ? /// 對話框標(biāo)題
? ? ? ? /// </summary>
? ? ? ? public string fileTittle;
? ? ? ? /// <summary>
? ? ? ? /// 文件過濾器 例如: “標(biāo)簽1|*.jpg|標(biāo)簽2|.png|標(biāo)簽3|.gif”
? ? ? ? /// </summary>
? ? ? ? public string fileFilter;
? ? ? ? /// <summary>
? ? ? ? /// 需要保存文件初始化的目錄
? ? ? ? /// </summary>
? ? ? ? public string fileInitialDirectory;
? ? ? ? /// <summary>
? ? ? ? /// 獲取的文件二進(jìn)制流
? ? ? ? /// </summary>
? ? ? ? public Stream inStream;
? ? }
? ? public class FileFilter
? ? {
? ? ? ? /// <summary>
? ? ? ? /// 圖片過濾器
? ? ? ? /// </summary>
? ? ? ? public const string pic = "圖片|*.jpg;*.png;*.gif;*.jpeg;*.bmp,";
? ? ? ? /// <summary>
? ? ? ? /// Excel過濾器
? ? ? ? /// </summary>
? ? ? ? public const string excel = "Excel files(*.xls)|*.xls|All files(*.*)|*.*";
? ? ? ? /// <summary>
? ? ? ? /// 音頻 過濾器
? ? ? ? /// </summary>
? ? ? ? public const string audio = "音頻文|*.mp3;*.wma;*.aac;*.midi;*.wav;*.aaa;*.bbb;*.ccc";
? ? }
}