1.打开对话框 要引用 System.Windows.Forms 注意 区分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>
/// 将二进制写入文件
/// </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>
/// 对话框标题
/// </summary>
public string fileTittle;
/// <summary>
/// 文件过滤器 例如: “标签1|*.jpg|标签2|.png|标签3|.gif”
/// </summary>
public string fileFilter;
/// <summary>
/// 需要保存文件初始化的目录
/// </summary>
public string fileInitialDirectory;
/// <summary>
/// 获取的文件二进制流
/// </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";
}
}
网友评论