unity 下載 文件保存到本地

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";

? ? }

}


demo下載地址

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末膏秫,一起剝皮案震驚了整個濱河市聊闯,隨后出現(xiàn)的幾起案子障斋,更是在濱河造成了極大的恐慌垃杖,老刑警劉巖羡洛,帶你破解...
    沈念sama閱讀 217,657評論 6 505
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件柑司,死亡現(xiàn)場離奇詭異,居然都是意外死亡拷呆,警方通過查閱死者的電腦和手機(jī)闲坎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,889評論 3 394
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來茬斧,“玉大人腰懂,你說我怎么就攤上這事∠畋” “怎么了悯恍?”我有些...
    開封第一講書人閱讀 164,057評論 0 354
  • 文/不壞的土叔 我叫張陵,是天一觀的道長伙狐。 經(jīng)常有香客問我涮毫,道長,這世上最難降的妖魔是什么贷屎? 我笑而不...
    開封第一講書人閱讀 58,509評論 1 293
  • 正文 為了忘掉前任罢防,我火速辦了婚禮,結(jié)果婚禮上唉侄,老公的妹妹穿的比我還像新娘咒吐。我一直安慰自己,他們只是感情好属划,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,562評論 6 392
  • 文/花漫 我一把揭開白布恬叹。 她就那樣靜靜地躺著,像睡著了一般同眯。 火紅的嫁衣襯著肌膚如雪绽昼。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,443評論 1 302
  • 那天须蜗,我揣著相機(jī)與錄音硅确,去河邊找鬼目溉。 笑死,一個胖子當(dāng)著我的面吹牛菱农,可吹牛的內(nèi)容都是我干的缭付。 我是一名探鬼主播,決...
    沈念sama閱讀 40,251評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼循未,長吁一口氣:“原來是場噩夢啊……” “哼陷猫!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起的妖,我...
    開封第一講書人閱讀 39,129評論 0 276
  • 序言:老撾萬榮一對情侶失蹤绣檬,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后羔味,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,561評論 1 314
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡钠右,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,779評論 3 335
  • 正文 我和宋清朗相戀三年赋元,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片飒房。...
    茶點(diǎn)故事閱讀 39,902評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡搁凸,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出狠毯,到底是詐尸還是另有隱情护糖,我是刑警寧澤,帶...
    沈念sama閱讀 35,621評論 5 345
  • 正文 年R本政府宣布嚼松,位于F島的核電站嫡良,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏献酗。R本人自食惡果不足惜寝受,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,220評論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望罕偎。 院中可真熱鬧很澄,春花似錦、人聲如沸颜及。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,838評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽俏站。三九已至讯蒲,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間肄扎,已是汗流浹背爱葵。 一陣腳步聲響...
    開封第一講書人閱讀 32,971評論 1 269
  • 我被黑心中介騙來泰國打工施戴, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人萌丈。 一個月前我還...
    沈念sama閱讀 48,025評論 2 370
  • 正文 我出身青樓赞哗,卻偏偏與公主長得像,于是被迫代替她去往敵國和親辆雾。 傳聞我的和親對象是個殘疾皇子肪笋,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,843評論 2 354

推薦閱讀更多精彩內(nèi)容

  • ```java /* * Copyright (C) 2006 The Android Open Source P...
    mrganer閱讀 1,143評論 0 50
  • adasd a uploadFileFromNativesduploadFileFromNativeuploadF...
    betterTry閱讀 538評論 0 1
  • ``` /* * ORACLE PROPRIETARY/CONFIDENTIAL. Use is subject ...
    非專業(yè)碼農(nóng)閱讀 336評論 0 0
  • 世上最牢固的感情 不是,我愛你 而是你習(xí)慣了有他 彼此依賴度迂,才是最深的相愛 一個人對你的好 并不是立刻就能看到的 ...
    信仰YHY閱讀 225評論 0 1
  • 如何阻止表單提交: JS中Array中如何增加數(shù)據(jù): unshift:將參數(shù)添加到原數(shù)組開頭藤乙,并返回數(shù)組的長度po...
    指尖核心閱讀 286評論 0 0