MVC .NET檔案下載 (傳值及另外開窗) (解決直接開檔導致串流錯誤)(KENDO UI)

其他
  1. MVC .NET檔案下載
  2. 傳值及另外開窗
  3. 解決直接開檔導致串流錯誤

主要遇到很多網站的教學不知道為什麼變成串流的方式,直接開檔導致亂碼。

以下為解決方式之一

首先是Controller的部分

               public ActionResult Open(string id)
        {
                //從專案根目錄到檔案路徑位置
                string file =  Server.MapPath("~/Uploads/Uploads/"); 
                string fileName = id;
                string filePath = file + fileName;//路径

                DirectoryInfo directory = new DirectoryInfo(filePath);
                return File(directory.FullName,System.Net.Mime.MediaTypeNames.Application.Octet, directory.Name);
        }

再來是VIEW前端的部分設置

function openSubmit(id) {
        var filename = id;
        window.open('../你的Controller/Open/?id='+id);
        }

主要連結方式就是指向controller的function
然後傳送自己要的字串
範例id就是我的檔名
如果要抓Kendo上的資訊可以使用data.id的方式直接抓取

另外要注意的就是set window的函式無法取用當前頁面的資訊
所以會有找不到的問題

這樣的方式應該也算安全,不過IIS上還是建議設定一個虛擬路徑去指向SERVER中的資料夾位置

Related Posts

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js