Webupload + MVC 之上傳下載

最近工做中用到了 MVC 的上傳及下載 , 寫下感覺javascript

本項目中用到的是百度的webuploadercss

 <!--引入Jquery--> 

<script src="~/Content/webUpLoade/jquery-1.11.3.js"></script>html

<!--引入CSS-->

<link href="~/Content/webUpLoade/webuploader.css" rel="stylesheet" />
java

<!--引入JS-->
<script src="~/Content/webUpLoade/webuploader.js"></script>


<script type="text/javascript">
var uploader = WebUploader.create({ 

// 選完文件後,是否自動上傳。
auto: false,jquery

swf: 'Content/webUpLoade/Uploader.swf',
// 文件接收服務端。
server: '/Home/TextUpdate',
// 開起分片上傳 chunkSize {Boolean} [可選] [默認值:5242880] 默認大小爲5M.
chunked: true,

//若是某個分片因爲網絡問題出錯,容許自動重傳多少次? Defualt 2
chunkRetry: 3,
// 選擇文件的按鈕。可選。
// 內部根據當前運行是建立,多是input元素,也多是flash.
pick: '#picker',web

//sendAsBinary: true,//開啓二進制
//fileVal: "uploadfile",// 默認file 指明參數名稱,後臺也用這個參數接收文件
// 文件格式限制
accept: {
title: '文件上傳',
extensions: 'rar,doc,xls',
mimeTypes: '.rar,.doc,.xls'
},
method: 'POST',網絡

 });
</script>

HTML 代碼

<div id="uploader" class="wu-example">
<!--用來存放文件信息-->
<div id="thelist" class="uploader-list"></div>
<div class="btns">
<div id="picker">選擇文件</div>
<button id="ctlBtn" class="btn btn-default">開始上傳</button>
</div>
</div>app

 

Controllerspa

[HttpPost]code

public ActionResult TextUpdate(HttpPostedFileBase file)
{
string filename = file.FileName;

string path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop), file.FileName);
file.SaveAs(path);
return View();
}

 

2.下載

view 視圖

<a href="/Document/DownFile?filePath=@item.Value&fileName=@item.Key">下載</a>  

 

Controller

一、

public ActionResult DownLoad(string path,string fileName)

{

return File(new FileStream(path, FileMode.Open), "application/octet-stream", Server.UrlEncode(fileName));

}

二、

public ActionResult DownFile(string filePath, string fileName)
{
filePath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["AttachmentPath"] + filePath);
FileStream fs = new FileStream(filePath, FileMode.Open);
byte[] bytes = new byte[(int)fs.Length];
fs.Read(bytes, 0, bytes.Length);
fs.Close();
Response.Charset = "UTF-8";
Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");
Response.ContentType = "application/octet-stream";

Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fileName));
Response.BinaryWrite(bytes);
Response.Flush();
Response.End();
return new EmptyResult();

}

相關文章
相關標籤/搜索