1. 程式人生 > >ASP.NET MVC 檔案上傳和檔案下載 以及 檔案下載的幾種方法

ASP.NET MVC 檔案上傳和檔案下載 以及 檔案下載的幾種方法

1、序言

最近專案中需要用到這個功能點,但是網上下載的時候總是出現亂碼。所以趁著這個時間自己整理出了一份,以後需要的時候就直接看自己的部落格就行了。已經測試過:在谷歌、火狐、IE等瀏覽器上都不會出現亂碼問題。

2、結果展示

2.1、上傳檔案成功介面

 

2.2、下載檔案成功介面

3、上傳檔案程式碼

3.1、View程式碼

複製程式碼
@model System.Web.HttpContextBase
@{
    ViewBag.Title = "上傳檔案";
}
<h2>上傳檔案</h2>
<br />
<br />
@*new { enctype = "multipart/form-data
" }比不可少,否則上傳檔案不會成功 *
@ @using (Html.BeginForm("Upload", "UploadFile", FormMethod.Post, new { enctype = "multipart/form-data" })) { <text>選擇上傳檔案:</text><input name="file" type="file" id="file" /> <br /> <br /> <input type="submit" name="Upload" value="Upload
" /> }
複製程式碼

3.2、Controller程式碼

複製程式碼
[HttpPost]
        public ActionResult Upload(FormCollection form)
        {
            if (Request.Files.Count == 0){
                //Request.Files.Count 檔案數為0上傳不成功
                return View();
            }
            var file = Request.Files[0];
            if (file.ContentLength == 0
){ //檔案大小大(以位元組為單位)為0時,做一些操作 return View(); } else{ //檔案大小不為0 file = Request.Files[0]; //儲存成自己的檔案全路徑,newfile就是你上傳後儲存的檔案, //伺服器上的UpLoadFile資料夾必須有讀寫許可權 string target = Server.MapPath("/")+("/Mock/Learning/");//取得目標資料夾的路徑 string filename = file.FileName;//取得檔名字 string path = target + filename;//獲取儲存的目標地址 file.SaveAs(path);} return View(); }
複製程式碼

4、下載檔案程式碼

4.1、View程式碼

<a href="/DownloadFile/[email protected]&fileName='小王子.pdf'">下載</a>

4.2、Controller程式碼

複製程式碼
public ActionResult Download(string filePath, string fileName)
        {
            Encoding encoding;
            string outputFileName = null;
            fileName = fileName.Replace("'", "");
            
            string browser =Request.UserAgent.ToUpper();
            if (browser.Contains("MS") == true && browser.Contains("IE") == true)
            {
                outputFileName = HttpUtility.UrlEncode(fileName);
                encoding =Encoding.Default;
            }
            else if (browser.Contains("FIREFOX") == true)
            {
                outputFileName = fileName;
                encoding =Encoding.GetEncoding("GB2312");
            }
            else
            {
                outputFileName = HttpUtility.UrlEncode(fileName);
                encoding = Encoding.Default;
            }
            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.ContentType = "application/octet-stream";
            Response.ContentEncoding = encoding;
            Response.AddHeader("Content-Disposition", "attachment; filename=" + outputFileName);
            Response.BinaryWrite(bytes);
            Response.Flush();
            Response.End();
            return new EmptyResult();
        }
複製程式碼

在ASP.net MVC 中有幾種下載檔案的方法

前提:要下載的檔案必須是在伺服器目錄中的,至於不在web專案server目錄中的檔案下載我不知道,但是還挺想了解的。

第一種:最簡單的超連結方法,<a>標籤的href直接指向目標檔案地址,這樣容易暴露地址造成盜鏈,這裡就不說了

第二種:後臺下載

在後臺下載中又可以細分為幾種下載方式

首先,在前臺,我們需要一個<a>標籤

<a href="~/Home/download">Click to get file</a>

Home為controller,download為action。

如果需要傳一些引數,可以:

<a href="~/Home/download?id=1">Click to get file</a>

在後臺:

(1)返回filestream

複製程式碼
public FileStreamResult download()
{
     string fileName = "aaa.txt";//客戶端儲存的檔名
     string filePath = Server.MapPath("~/Document/123.txt");//路徑
     return File(new FileStream(filePath, FileMode.Open), "text/plain",   
     fileName);
}
複製程式碼

其中:“text/plain”是檔案MIME型別

(2)返回file

public FileResult download()
        {
            string filePath = Server.MapPath("~/Document/123.txt");//路徑
            return File(filePath, "text/plain", "welcome.txt"); //welcome.txt是客戶端儲存的名字
        }

(3)TransmitFile方法

複製程式碼
 1  public void download()
 2 {
 3       string fileName = "aaa.txt";//客戶端儲存的檔名
 4       string filePath = Server.MapPath("~/Document/123.txt");//路徑
 5       FileInfo fileinfo = new FileInfo(filePath);
 6             Response.Clear();         //清除緩衝區流中的所有內容輸出
 7             Response.ClearContent();  //清除緩衝區流中的所有內容輸出
 8             Response.ClearHeaders();  //清除緩衝區流中的所有頭
 9             Response.Buffer = true;   //該值指示是否緩衝輸出,並在完成處理整個響應之後將其傳送
10             Response.AddHeader("Content-Disposition", "attachment;filename=" + fileName);
11             Response.AddHeader("Content-Length",fileinfo.Length.ToString());
12             Response.AddHeader("Content-Transfer-Encoding", "binary");
13             Response.ContentType = "application/unknow";  //獲取或設定輸出流的 HTTP MIME 型別
14             Response.ContentEncoding = System.Text.Encoding.GetEncoding("gb2312"); //獲取或設定輸出流的 HTTP 字符集
15             Response.TransmitFile(filePath);
16             Response.End();
17 }
複製程式碼

(4)Response分塊下載

複製程式碼
 1 public void download()
 2         {
 3             string fileName = "aaa.txt";//客戶端儲存的檔名
 4             string filePath = Server.MapPath("~/Document/123.txt");//路徑
 5 
 6             System.IO.FileInfo fileInfo = new System.IO.FileInfo(filePath);
 7             if (fileInfo.Exists == true)
 8             {
 9                 const long ChunkSize = 102400;//100K 每次讀取檔案,只讀取100K,這樣可以緩解伺服器的壓力
10                 byte[] buffer = new byte[ChunkSize];
11 
12                 Response.Clear();
13                 System.IO.FileStream iStream = System.IO.File.OpenRead(filePath);
14                 long dataLengthToRead = iStream.Length;//獲取下載的檔案總大小
15                 Response.ContentType = "application/octet-stream";
16                 Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName));
17                 while (dataLengthToRead > 0 && Response.IsClientConnected)
18                 {
19                     int lengthRead = iStream.Read(buffer, 0, Convert.ToInt32(ChunkSize));//讀取的大小
20                     Response.OutputStream.Write(buffer, 0, lengthRead);
21                     Response.Flush();
22                     dataLengthToRead = dataLengthToRead - lengthRead;
23                 }
24                 Response.Close();
25             }         
26         }
複製程式碼

<input>+ajax方法

要重點說說這個方法,ajax返回不了檔案流,所以說用ajax呼叫上面任意一種後臺方法都要出問題,下載不了檔案。

所以,只能讓後臺返回所需下載檔案的url地址,然後呼叫windows.location.href。

優點:ajax可以傳好幾個引數(當然以json形式),傳100個都無所謂。你要是用<a href="網址?引數=值"></a>的方法傳100得寫死。。。(公司需求,至少要傳100多個引數)

缺點:支援下載exe,rar,msi等型別檔案。對於txt則會直接開啟,慎用!對於其他不常用的型別檔案則直接報錯。

        所以我的建議是得到多個引數,通過資料庫找到所有需要下載的檔案然後壓縮打包,然後返回url下載。(你要是一個一個給使用者下,使用者都瘋了)

        那麼問題又來了,C#怎麼用程式碼實現將本地的一些檔案打包壓縮到伺服器目錄下呢?這我不知道。

           因為你只是單純的放在目錄資料夾下沒有用的,我們平時在伺服器某目錄下新增某一個檔案都是右鍵,新增XXX項這樣,這樣才能真正的將檔案放在伺服器中。

           可是純程式碼該怎麼實現呢??

        *可能的解決方法:先在專案目錄下放一個空的rar檔案或者沒什麼功能的exe,msi檔案,然後在後臺打包完一些檔案後去替換它,不知道可行不。

            (1)首先清空原壓縮包中的內容

            (2)將檔案壓縮到壓縮包中

            (3)返回 XXX.rar

前端:

<input type="button" id="downloadbutton"/>

ajax:

複製程式碼
$("#downloadbutton").click(function () {
                $.ajax({
                    type: "GET",
                    url: "/Home/download",
                    data: { id: "1" },
                    dataType:"json",
                    success: function (result) {
                        window.location.target = "_blank";
                        window.location.href = result;                      
                    }
                })
            })
複製程式碼

後臺:

public string download(int id)
        {
            string filePath = "Document/123.msi";//路徑
            return filePath;
        }

這裡,id是沒有什麼作用的,後臺就別用什麼server.Mappath了,肯定錯。直接寫伺服器專案資料夾/檔名 即可。


前言

這一節我們來講講在MVC中如何進行檔案的上傳,我們逐步深入,一起來看看。

Upload File(一)

我們在預設建立的專案中的Home控制器下新增如下:

複製程式碼
        public ActionResult UploadFile()
        {
            return View();
        }

        [HttpPost]
        public ActionResult UploadFile(HttpPostedFileBase file)
        {
            var fileName = file.FileName;
            var filePath = Server.MapPath(string.Format("~/{0}", "File"));
            file.SaveAs(Path.Combine(filePath, fileName));
            return View();
        }
複製程式碼

在 UploadFile 檢視中新增上如下:

<form action="/Home/UploadFile" method="post" enctype="multipart/form-data">
    <input type="file" name="file" /><br />
    <input type="submit" value="提交" />
</form>

有關檢視中我們就不必多說,只需明白如下兩點:

(1)在後臺利用HttpPostedFileBase來接收上傳檔案,該類為一個抽象類,但在ASP.NET Web Form卻沒有此類,此類的出現是為了更好的進行單元測試。

(2)在檢視中檔案型別的name要和後臺接收檔案的引數一致。

接下來我們進行演示看看結果:

 上述我們簡單的上傳了一個Excel檔案,下面我們通過強型別檢視以及模型驗證來強化上傳。

Upload File(二)

我們建立如下BlogModel類:

複製程式碼
    public class BlogModel
    {
        [Display(Name = "部落格名稱")]
        [Required(ErrorMessage = "請輸入你的部落格名稱!")]
        public string BlogName { get; set; }

        [Display(Name = "部落格地址")]
        [Required(ErrorMessage = "請輸入你的部落格地址!")]
        public string BlogAddress { get; set; }

        [Display(Name = "部落格圖片")]
        [Required(ErrorMessage = "請上傳你的部落格圖片!")]
        [ValidateFile]
        public HttpPostedFileBase BlogPhoto { get; set; }
    }

相關推薦

ASP.NET操作Excel---Excel後解析Excel檔案

<tr> <td width="16%" class="c_tdleft"> 上傳Excel檔案: </td>

NoSql-MongoDB GridFS+ASP.NET MVC實現,顯示

namespace MongoDBTest.Controllers { public class MongoDBHelperController : Controller { private static MongoDatabase DB; public sta

ASP.NET MVC 圖片(最基本的例子)

    CSDN廣告是越來越多了,所有部落格筆記不再更新,新網址 DotNet筆記 html程式碼:<body> <div> <form act

ASP.NET MVC圖片前預覽

今年的學習計劃是學好ASP.NET MVC和jQuery,多練習,不懂的去網上找或是多看書! 早上有練習圖片上傳前預覽並獲取圖片檔名和圖片位元組大小。 在控制器中建立一個Action: 在Views目錄下對應的控制器名稱下建立檢視PreViewing: 上圖中,標記1

ASP.NET MVC 圖片

author: Yeshen time:2016.11.11 http://blog.csdn.net/yeshennet 可以這樣做: 在程式碼目錄下新建upload資料夾 Controller:

ASP.NET MVC 檔案檔案下載 以及 檔案下載方法

1、序言最近專案中需要用到這個功能點,但是網上下載的時候總是出現亂碼。所以趁著這個時間自己整理出了一份,以後需要的時候就直接看自己的部落格就行了。已經測試過:在谷歌、火狐、IE等瀏覽器上都不會出現亂碼問題。2、結果展示2.1、上傳檔案成功介面 2.2、下載檔案成功介面3、上傳

asp.net 檔案下載管理原始碼

    利用asp.net進行檔案上傳和下載時非常常用的功能,現做整理,將原始碼上傳,提供給初學者參考,以下程式碼中的樣式檔案就不上傳了,下載者請將樣式去掉。 效果圖如下: <%@ Page Language="C#" AutoEventWireu

ASP.NET實現檔案下載

###### 本文的開發配置 ###### .NET版本:.NET Framework 4.0 開發環境:Microsoft Visual Studio 2013 瀏覽器:IE、Chrome、FireFox等都行   1、搭建網站結構 建立一個新的目錄

ASP.NET檔案下載

aspx頁面 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="上傳和下載檔案.aspx.cs" Inherits="上傳和下載檔案" %> <!DOCTYPE html PUBLIC "-/

Spring MVC檔案下載

檔案上傳 檔案上傳需將表格的提交方式設為"POST",並且將enctype設為"multipart/form-data",以二進位制的方式提交資料。 spring mvc中可通過MultipartResolver監聽每個請求,如有上傳的檔案,則把請求封裝為MultipartH

Spring MVC檔案下載

“君看一葉舟,出沒風波里” 使用Spring MVC 進行檔案的上傳和下載,除了Spring MVC 的jar包之外,還需用到commons-fileupload-1.2.2.jar、commons-io-2.1.jar兩個包。這裡已給出下載地址:jar包下載地

asp檔案下載

ASP.NET實現上傳檔案        前端        介面十分簡單,只是放一個file型別的<input>和一個按鈕,並且為這個按鈕新增點選事件(btnUpLoad_Click),如下圖:

ASP.NET FileUpload讀取檔案的內容

好久沒更了,來一個 前臺: <form id="MengXianhui" runat="server"> <asp:FileUpload ID="GetUploadFileC

.net mvc ajax 檔案

1、前端 <div> <input type="file" id="upfile" /> <button type="button" id="btn"> 上 傳 </button> </div> $

從.Net到Java學習第十篇——Spring Boot檔案下載

圖片上傳 Spring Boot中的檔案上傳就是Spring MVC中的檔案上傳,將其整合進來了。 在模板目錄建立一個新的頁面 profile/uploadPage.html <!DOCTYPE html> <html xmlns:th="http://www.thymel

asp.net利用HttpWorkerRequest檔案

前臺頁面 <form id="form1" runat="server" encType="multipart/form-data" method="post">     <div>         <INPUT id="firstFile"

spring MVC檔案實時進度提醒

@RequestMapping(value = "/getProgress.do", method = RequestMethod.POST, produces = { "application/json" }) @ResponseBody

Asp.net使用form檔案

1 – 頁面程式碼: uploadfile.html <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>&

javaExcel檔案下載

上傳在頁面必須加上下面屬性 <form method="post" enctype="multipart/form-data" target="frameFile" action="${vehiclePath }/bindVehicle?${_csrf.parameterName}=$

SpringBoot多檔案檔案下載

1、前端的form表單: <form id="form"  action="controller層的多檔案上傳方法訪問路徑" method="post" enctype="multipart/form-data"> <input  type="file" n