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/Download?filePath=@ViewBag.Value&fileName='小王子.pdf'">下载</a>

4.2、Controller代码

复制代码
public ActionResult Download(string filePath, string fileName)
        {
            Encoding encoding;
            string outputFileName = ;
            fileName = fileName.Replace("'", "");
        </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">string</span> browser =<span style="margin:0px;padding:0px;line-height:1.5;">Request.UserAgent.ToUpper();
        </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">if</span> (browser.Contains(<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">MS</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span>) == <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">true</span> &amp;&amp; browser.Contains(<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">IE</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span>) == <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">true</span><span style="margin:0px;padding:0px;line-height:1.5;">)
        {
            outputFileName </span>=<span style="margin:0px;padding:0px;line-height:1.5;"> HttpUtility.UrlEncode(fileName);
            encoding </span>=<span style="margin:0px;padding:0px;line-height:1.5;">Encoding.Default;
        }
        </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">else</span> <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">if</span> (browser.Contains(<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">FIREFOX</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span>) == <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">true</span><span style="margin:0px;padding:0px;line-height:1.5;">)
        {
            outputFileName </span>=<span style="margin:0px;padding:0px;line-height:1.5;"> fileName;
            encoding </span>=Encoding.GetEncoding(<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">GB2312</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;line-height:1.5;">);
        }
        </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">else</span><span style="margin:0px;padding:0px;line-height:1.5;">
        {
            outputFileName </span>=<span style="margin:0px;padding:0px;line-height:1.5;"> HttpUtility.UrlEncode(fileName);
            encoding </span>=<span style="margin:0px;padding:0px;line-height:1.5;"> Encoding.Default;
        }
        FileStream fs </span>= <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">new</span><span style="margin:0px;padding:0px;line-height:1.5;"> FileStream(filePath, FileMode.Open);
        </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">byte</span>[] bytes = <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">new</span> <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">byte</span>[(<span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">int</span><span style="margin:0px;padding:0px;line-height:1.5;">)fs.Length];
        fs.Read(bytes, </span><span style="margin:0px;padding:0px;color:rgb(128,0,128);line-height:1.5;">0</span><span style="margin:0px;padding:0px;line-height:1.5;">, bytes.Length);
        fs.Close();
        Response.Charset </span>= <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">UTF-8</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;line-height:1.5;">;
        Response.ContentType </span>= <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">application/octet-stream</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;line-height:1.5;">;
        Response.ContentEncoding </span>=<span style="margin:0px;padding:0px;line-height:1.5;"> encoding;
        Response.AddHeader(</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">Content-Disposition</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span>, <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">attachment; filename=</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span> +<span style="margin:0px;padding:0px;line-height:1.5;"> outputFileName);
        Response.BinaryWrite(bytes);
        Response.Flush();
        Response.End();
        </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">return</span> <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">new</span><span style="margin:0px;padding:0px;line-height:1.5;"> EmptyResult();
    }</span></pre><div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"><span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><a title="复制代码" style="margin:0px;padding:0px;color:#000000;border:none;" target="_blank"><img src="https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:700px;border:none;"></a></span></div></div><p style="margin:10px auto;">&nbsp;</p></div><div class="clear" style="margin:0px;padding:0px;clear:both;color:rgb(0,0,0);font-family:'Helvetica Neue', Helvetica, Verdana, Arial, sans-serif;font-size:14px;text-align:left;background-color:rgb(255,255,255);"></div><div style="margin:20px 0px 0px;padding:0px;color:rgb(0,0,0);font-family:'Helvetica Neue', Helvetica, Verdana, Arial, sans-serif;font-size:14px;text-align:left;background-color:rgb(255,255,255);"><div style="margin:0px 0px 10px;padding:0px;color:rgb(102,102,102);"></div><div style="margin-top:0px;margin-right:0px;margin-bottom:0px;margin-left:0px;padding:0px;color:rgb(102,102,102);"><p style="margin:10px auto;color:rgb(75,75,75);font-family:Verdana, Arial, Helvetica, sans-serif;font-size:13px;background-color:rgb(255,255,255);">在ASP.net MVC 中有几种下载文件的方法</p><p style="margin:10px auto;color:rgb(75,75,75);font-family:Verdana, Arial, Helvetica, sans-serif;font-size:13px;background-color:rgb(255,255,255);"><strong><span style="color:rgb(255,0,0);">前提:</span></strong>要下载的文件必须是在服务器目录中的,至于不在web项目server目录中的文件下载我不知道,但是还挺想了解的。</p><p style="margin:10px auto;color:rgb(75,75,75);font-family:Verdana, Arial, Helvetica, sans-serif;font-size:13px;background-color:rgb(255,255,255);">&nbsp;</p><p style="margin:10px auto;color:rgb(75,75,75);font-family:Verdana, Arial, Helvetica, sans-serif;font-size:13px;background-color:rgb(255,255,255);">第一种:最简单的超链接方法,&lt;a&gt;标签的href直接指向目标文件地址,这样容易暴露地址造成盗链,这里就不说了</p><p style="margin:10px auto;color:rgb(75,75,75);font-family:Verdana, Arial, Helvetica, sans-serif;font-size:13px;background-color:rgb(255,255,255);">第二种:后台下载</p><p style="margin:10px auto;color:rgb(75,75,75);font-family:Verdana, Arial, Helvetica, sans-serif;font-size:13px;background-color:rgb(255,255,255);">&nbsp;</p><p style="margin:10px auto;color:rgb(75,75,75);font-family:Verdana, Arial, Helvetica, sans-serif;font-size:13px;background-color:rgb(255,255,255);">在后台下载中又可以细分为几种下载方式</p><p style="margin:10px auto;color:rgb(75,75,75);font-family:Verdana, Arial, Helvetica, sans-serif;font-size:13px;background-color:rgb(255,255,255);">首先,在前台,我们需要一个&lt;a&gt;标签</p><div class="cnblogs_code" style="background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);padding:5px;margin:5px 0px;color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"><pre style="margin-bottom:0px;white-space:pre-wrap;font-family:'Courier New';">&lt;a href=<span style="color:rgb(128,0,0);line-height:1.5;">"</span><span style="color:rgb(128,0,0);line-height:1.5;">~/Home/download</span><span style="color:rgb(128,0,0);line-height:1.5;">"</span>&gt;Click to <span style="color:rgb(0,0,255);line-height:1.5;">get</span> file&lt;/a&gt;</pre></div><p style="margin:10px auto;color:rgb(75,75,75);font-family:Verdana, Arial, Helvetica, sans-serif;font-size:13px;background-color:rgb(255,255,255);">Home为controller,download为action。</p><p style="margin:10px auto;color:rgb(75,75,75);font-family:Verdana, Arial, Helvetica, sans-serif;font-size:13px;background-color:rgb(255,255,255);">如果需要传一些参数,可以:</p><div class="cnblogs_code" style="background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);padding:5px;margin:5px 0px;color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"><pre style="margin-bottom:0px;white-space:pre-wrap;font-family:'Courier New';">&lt;a href=<span style="color:rgb(128,0,0);line-height:1.5;">"</span><span style="color:rgb(128,0,0);line-height:1.5;">~/Home/download?id=1</span><span style="color:rgb(128,0,0);line-height:1.5;">"</span>&gt;Click to <span style="color:rgb(0,0,255);line-height:1.5;">get</span> file&lt;/a&gt;</pre></div><p style="margin:10px auto;color:rgb(75,75,75);font-family:Verdana, Arial, Helvetica, sans-serif;font-size:13px;background-color:rgb(255,255,255);">&nbsp;</p><p style="margin:10px auto;color:rgb(75,75,75);font-family:Verdana, Arial, Helvetica, sans-serif;font-size:13px;background-color:rgb(255,255,255);">在后台:</p><p style="margin:10px auto;color:rgb(75,75,75);font-family:Verdana, Arial, Helvetica, sans-serif;font-size:13px;background-color:rgb(255,255,255);">(1)返回filestream</p><div class="cnblogs_code" style="background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);padding:5px;margin:5px 0px;color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"><div class="cnblogs_code_toolbar" style="margin-top:5px;"><span class="cnblogs_code_copy" style="padding-right:5px;line-height:1.5;"><a title="复制代码" style="color:rgb(52,104,164);border:none;" target="_blank"><img src="https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif" alt="复制代码" style="max-width:900px;border:none;"></a></span></div><pre style="margin-bottom:0px;white-space:pre-wrap;font-family:'Courier New';"><span style="color:rgb(0,0,255);line-height:1.5;">public</span><span style="line-height:1.5;"> 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]
    </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">public</span><span style="margin:0px;padding:0px;line-height:1.5;"> ActionResult UploadFile(HttpPostedFileBase file)
    {
        </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">var</span> fileName =<span style="margin:0px;padding:0px;line-height:1.5;"> file.FileName;
        </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">var</span> filePath = Server.MapPath(<span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">string</span>.Format(<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">~/{0}</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span>, <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">File</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;line-height:1.5;">));
        file.SaveAs(Path.Combine(filePath, fileName));
        </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">return</span><span style="margin:0px;padding:0px;line-height:1.5;"> View();
    }</span></pre><div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"><span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><a title="复制代码" style="margin:0px;padding:0px;color:rgb(45,161,45);border:none;" target="_blank"><img src="https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:920px;border:none;"></a></span></div></div><p style="margin:10px auto;color:rgb(17,17,17);font-family:'Helvetica Neue', Arial;font-size:13px;">在&nbsp;<span class="cnblogs_code" style="margin:5px 0px;padding:3px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;">UploadFile</span>&nbsp;视图中添加上如下:</p><div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"><pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';"><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">&lt;</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">form </span><span style="margin:0px;padding:0px;color:rgb(255,0,0);line-height:1.5;">action</span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">="/Home/UploadFile"</span><span style="margin:0px;padding:0px;color:rgb(255,0,0);line-height:1.5;"> method</span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">="post"</span><span style="margin:0px;padding:0px;color:rgb(255,0,0);line-height:1.5;"> enctype</span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">="multipart/form-data"</span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">&gt;</span>
<span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">&lt;</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">input </span><span style="margin:0px;padding:0px;color:rgb(255,0,0);line-height:1.5;">type</span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">="file"</span><span style="margin:0px;padding:0px;color:rgb(255,0,0);line-height:1.5;"> name</span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">="file"</span> <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">/&gt;&lt;</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">br </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">/&gt;</span>
<span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">&lt;</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">input </span><span style="margin:0px;padding:0px;color:rgb(255,0,0);line-height:1.5;">type</span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">="submit"</span><span style="margin:0px;padding:0px;color:rgb(255,0,0);line-height:1.5;"> value</span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">="提交"</span> <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">/&gt;</span>

</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 </span>= <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">博客地址</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;line-height:1.5;">)]
    [Required(ErrorMessage </span>= <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">请输入你的博客地址!</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;line-height:1.5;">)]
    </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">public</span> <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">string</span> BlogAddress { <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">get</span>; <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">set</span><span style="margin:0px;padding:0px;line-height:1.5;">; }

    [Display(Name </span>= <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">博客图片</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;line-height:1.5;">)]
    [Required(ErrorMessage </span>= <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">请上传你的博客图片!</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;line-height:1.5;">)]
   <span style="margin:0px;padding:0px;color:rgb(255,0,0);line-height:1.5;"> [ValidateFile]
    </span></span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">public</span> HttpPostedFileBase BlogPhoto { <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">get</span>; <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">set</span><span style="margin:0px;padding:0px;line-height:1.5;">; }
}</span></pre><div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"><span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><a title="复制代码" style="margin:0px;padding:0px;color:rgb(45,161,45);border:none;" target="_blank"><img src="https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:920px;border:none;"></a></span></div></div><p style="margin:10px auto;color:rgb(17,17,17);font-family:'Helvetica Neue', Arial;font-size:13px;">上述未有验证文件的特性,只能自定义文件特性,如下:</p><div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"><div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"><span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><a title="复制代码" style="margin:0px;padding:0px;color:rgb(45,161,45);border:none;" target="_blank"><img src="https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:920px;border:none;"></a></span></div><pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';">    <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">public</span> <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">class</span><span style="margin:0px;padding:0px;line-height:1.5;"> ValidateFileAttribute : ValidationAttribute
{
    </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">public</span> <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">override</span> <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">bool</span> IsValid(<span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">object</span><span style="margin:0px;padding:0px;line-height:1.5;"> value)
    {
        </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">int</span> MaxContentLength = <span style="margin:0px;padding:0px;color:rgb(128,0,128);line-height:1.5;">1024</span> * <span style="margin:0px;padding:0px;color:rgb(128,0,128);line-height:1.5;">1024</span> * <span style="margin:0px;padding:0px;color:rgb(128,0,128);line-height:1.5;">4</span><span style="margin:0px;padding:0px;line-height:1.5;">;
        </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">string</span>[] AllowedFileExtensions = <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">new</span> <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">string</span>[] { <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">.jpg</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span>, <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">.gif</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span>, <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">.png</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span>, <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">.pdf</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;line-height:1.5;"> };

        </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">var</span> file = value <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">as</span><span style="margin:0px;padding:0px;line-height:1.5;"> HttpPostedFileBase;

        </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">if</span> (file == <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">null</span><span style="margin:0px;padding:0px;line-height:1.5;">)
            </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">return</span> <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">false</span><span style="margin:0px;padding:0px;line-height:1.5;">;
        </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">else</span> <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">if</span> (!AllowedFileExtensions.Contains(file.FileName.Substring(file.FileName.LastIndexOf(<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">.</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">'</span><span style="margin:0px;padding:0px;line-height:1.5;">))))
        {
            ErrorMessage </span>= <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">请上传你的博客图片类型: </span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span> + <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">string</span>.Join(<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">, </span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;line-height:1.5;">, AllowedFileExtensions);
            </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">return</span> <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">false</span><span style="margin:0px;padding:0px;line-height:1.5;">;
        }
        </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">else</span> <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">if</span> (file.ContentLength &gt;<span style="margin:0px;padding:0px;line-height:1.5;"> MaxContentLength)
        {
            ErrorMessage </span>= <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">上传图片过大,不能超过4兆 : </span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span> + (MaxContentLength / <span style="margin:0px;padding:0px;color:rgb(128,0,128);line-height:1.5;">1024</span>).ToString() + <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">MB</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;line-height:1.5;">;
            </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">return</span> <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">false</span><span style="margin:0px;padding:0px;line-height:1.5;">;
        }
        </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">else</span>
            <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">return</span> <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">true</span><span style="margin:0px;padding:0px;line-height:1.5;">;
    }
}</span></pre><div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"><span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><a title="复制代码" style="margin:0px;padding:0px;color:rgb(45,161,45);border:none;" target="_blank"><img src="https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:920px;border:none;"></a></span></div></div><p style="margin:10px auto;color:rgb(17,17,17);font-family:'Helvetica Neue', Arial;font-size:13px;">我们可以任意设置上传的文件大小,我们设置为40兆,在配置文件中我们知道&nbsp;<span class="cnblogs_code" style="margin:5px 0px;padding:3px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;">maxRequestLength =&nbsp;<span style="margin:0px;padding:0px;color:rgb(128,0,128);line-height:1.5;">4096</span></span>&nbsp;默认是4兆,当然我们可以改变其默认设置。</p><div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"><pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';">    &lt;httpRuntime targetFramework=<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">4.5</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span> executionTimeout=<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">1100</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span>  maxRequestLength=<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">40960</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span> /&gt;</pre></div><p style="margin:10px auto;color:rgb(17,17,17);font-family:'Helvetica Neue', Arial;font-size:13px;">此时我们接着在控制器中修改上述上传的方法:</p><div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"><div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"><span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><a title="复制代码" style="margin:0px;padding:0px;color:rgb(45,161,45);border:none;" target="_blank"><img src="https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:920px;border:none;"></a></span></div><pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';"><span style="margin:0px;padding:0px;line-height:1.5;">        [HttpPost]
    </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">public</span><span style="margin:0px;padding:0px;line-height:1.5;"> ActionResult UploadFile(BlogModel bModel)
    {
        </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">if</span><span style="margin:0px;padding:0px;line-height:1.5;"> (ModelState.IsValid)
        {
            </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">var</span> fileName =<span style="margin:0px;padding:0px;line-height:1.5;"> bModel.BlogPhoto.FileName;
            </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">var</span> filePath = Server.MapPath(<span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">string</span>.Format(<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">~/{0}</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span>, <span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">File</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;line-height:1.5;">));
            bModel.BlogPhoto.SaveAs(Path.Combine(filePath, fileName));
            ModelState.Clear();
        }          
        </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">return</span><span style="margin:0px;padding:0px;line-height:1.5;"> View();
    }</span></pre><div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"><span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><a title="复制代码" style="margin:0px;padding:0px;color:rgb(45,161,45);border:none;" target="_blank"><img src="https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:920px;border:none;"></a></span></div></div><p style="margin:10px auto;color:rgb(17,17,17);font-family:'Helvetica Neue', Arial;font-size:13px;">我们接下来看看效果:</p><p style="margin:10px auto;color:rgb(17,17,17);font-family:'Helvetica Neue', Arial;font-size:13px;"><img src="https://images2015.cnblogs.com/blog/589642/201604/589642-20160421154204804-831769151.gif" alt="" style="padding:0px;border:0px;max-width:920px;"></p><p style="margin:10px auto;color:rgb(17,17,17);font-family:'Helvetica Neue', Arial;font-size:13px;">咋回事,出状况了看来是我们的文件过大的原因,看了下该文件有接近45兆,而我们却设置的是40兆,于是乎继续在配置文件中去修改文件大小,但是结果还是一样。我们继续仔细看看该结果的提示,根据提示去找到配置文件下的节点再试试,我们在&nbsp;<span class="cnblogs_code" style="margin:5px 0px;padding:3px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;">syste.webServer</span>&nbsp;节点下设置为2G:</p><div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"><div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"><span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><a title="复制代码" style="margin:0px;padding:0px;color:rgb(45,161,45);border:none;" target="_blank"><img src="https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:920px;border:none;"></a></span></div><pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';">    &lt;security&gt;
  &lt;requestFiltering&gt;
    &lt;requestLimits maxAllowedContentLength=<span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">2147483647</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">"</span>&gt;
    &lt;/requestLimits&gt;
  &lt;/requestFiltering&gt;
&lt;/security&gt;</pre><div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"><span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><a title="复制代码" style="margin:0px;padding:0px;color:rgb(45,161,45);border:none;" target="_blank"><img src="https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:920px;border:none;"></a></span></div></div><p style="margin:10px auto;color:rgb(17,17,17);font-family:'Helvetica Neue', Arial;font-size:13px;">结果就好使了,查了查也有遇到类似问题的人,貌似只有给个结果,却没有给解释,为什么在&nbsp;<span class="cnblogs_code" style="margin:5px 0px;padding:3px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;">httpRuntime</span>&nbsp;中设置不行,但是有些这样设置是正确的,这是什么原因?最终找到了答案:</p><p style="margin:10px auto;color:rgb(17,17,17);font-family:'Helvetica Neue', Arial;font-size:13px;"><span style="margin:0px;padding:0px;color:rgb(128,128,0);">(1)在IIS 5和IIS 6中,默认文件上传的最大为4兆,当上传的文件大小超过4兆时,则会得到错误信息,但是我们通过如下来设置文件大小。</span></p><div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"><pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';">&lt;system.web&gt;

<httpRuntime maxRequestLength=“2147483647” executionTimeout=“100000” />
</system.web>

(2)在IIS 7中,默认文件上传的最大为28.6兆,当超过其默认设置大小,同样会得到错误信息,但是我们却可以通过如下来设置文件上传大小。

复制代码
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength=“2147483647” />
</requestFiltering>
</security>
</system.webServer>
复制代码

【类推的话,个人觉得可能是在IIS 7+以上都是通过如上述IIS 7来设置文件上传大小】

虽然我们在服务器端对其进行验证,但是我们觉得这样还是不能保险,我们继续在客户端对其上传的图片类型和大小进行验证。 

(1)利用强类型视图给出视图代码:

复制代码
<style type=“text/css”>
.field-validation-error {
color: red;
}
</style>
<form id=“uploadFileSub” action="/Home/UploadFile" method=“post” enctype=“multipart/form-data”>
<fieldset>
<legend></legend>
<ul class=“lifile”>
<li>
@Html.LabelFor(m => m.BlogName)<br />
@Html.TextBoxFor(m => m.BlogName, new { maxlength = 50 })
@Html.ValidationMessageFor(m => m.BlogName)
</li>
<li>
@Html.LabelFor(m => m.BlogAddress)<br />
@Html.TextBoxFor(m => m.BlogAddress, new { maxlength = 200 })
@Html.ValidationMessageFor(m => m.BlogAddress)<br />
</li>
<li>
@Html.LabelFor(m => m.BlogPhoto)
@Html.TextBoxFor(m => m.BlogPhoto, new { type = “file” })
@Html.ValidationMessageFor(m => m.BlogPhoto)
<span id=“warning” style=“color:red;font-size:large;”></span>
</li>
<li>
<input type=“submit” value=“提交” />
</li>
</ul>
<span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">&lt;/</span><span style="margin:0px;padding:0px;color:rgb(128,0,0);line-height:1.5;">fieldset</span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">&gt;</span>

</form>

复制代码

(2)利用脚本获取上传文件大小:

复制代码
    function GetFileSize(fileid) {
var fileSize = 0;
fileSize = KaTeX parse error: Expected 'EOF', got '#' at position 3: ("#̲" + fileid)[0].…/);
var strName = objRE.exec(strFilepath);
    </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">if</span> (strName == <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">null</span><span style="margin:0px;padding:0px;line-height:1.5;">) {
        </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">return</span> <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">null</span><span style="margin:0px;padding:0px;line-height:1.5;">;
    }
    </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">else</span><span style="margin:0px;padding:0px;line-height:1.5;"> {
        </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">return</span> strName[0<span style="margin:0px;padding:0px;line-height:1.5;">];
    }
}</span></pre><div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"><span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><a title="复制代码" style="margin:0px;padding:0px;color:rgb(45,161,45);border:none;" target="_blank"><img src="https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:920px;border:none;"></a></span></div></div><p style="margin:10px auto;color:rgb(17,17,17);font-family:'Helvetica Neue', Arial;font-size:13px;"><span style="margin:0px;padding:0px;color:rgb(128,128,0);">(4)当更换文件时触发Change事件对其文件类型和文件大小进行验证:</span></p><div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"><div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"><span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><a title="复制代码" style="margin:0px;padding:0px;color:rgb(45,161,45);border:none;" target="_blank"><img src="https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:920px;border:none;"></a></span></div><pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';">        $("#BlogPhoto").change(<span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">function</span><span style="margin:0px;padding:0px;line-height:1.5;"> () {
        </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">var</span> file = getNameFromPath($(<span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">this</span><span style="margin:0px;padding:0px;line-height:1.5;">).val());
        </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">if</span> (file != <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">null</span><span style="margin:0px;padding:0px;line-height:1.5;">) {
            </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">var</span> errors = $(document).find(".field-validation-error"<span style="margin:0px;padding:0px;line-height:1.5;">);
            $.each(errors, </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">function</span><span style="margin:0px;padding:0px;line-height:1.5;"> (k, v) {
                </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">if</span> ($(v).attr("data-valmsg-for") === "BlogPhoto"<span style="margin:0px;padding:0px;line-height:1.5;">) {
                    $(v).hide();
                }
            });
            </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">var</span> extension = file.substr((file.lastIndexOf('.') + 1<span style="margin:0px;padding:0px;line-height:1.5;">));
            </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">switch</span><span style="margin:0px;padding:0px;line-height:1.5;"> (extension) {
                </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">case</span> 'jpg'<span style="margin:0px;padding:0px;line-height:1.5;">:
                </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">case</span> 'png'<span style="margin:0px;padding:0px;line-height:1.5;">:
                </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">case</span> 'gif'<span style="margin:0px;padding:0px;line-height:1.5;">:
                </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">case</span> 'pdf'<span style="margin:0px;padding:0px;line-height:1.5;">:
                    fileTypeBool </span>= <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">false</span><span style="margin:0px;padding:0px;line-height:1.5;">;
                    </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">break</span><span style="margin:0px;padding:0px;line-height:1.5;">;
                </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">default</span><span style="margin:0px;padding:0px;line-height:1.5;">:
                    fileTypeBool </span>= <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">true</span><span style="margin:0px;padding:0px;line-height:1.5;">;
            }
        }
        </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">if</span><span style="margin:0px;padding:0px;line-height:1.5;"> (fileTypeBool) {
            $(</span>"#warning").html("只能上传扩展名为jpg,png,gif,pdf的文件!"<span style="margin:0px;padding:0px;line-height:1.5;">);
            </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">return</span> <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">false</span><span style="margin:0px;padding:0px;line-height:1.5;">;
        }
        </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">else</span><span style="margin:0px;padding:0px;line-height:1.5;"> {
            </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">var</span> size = GetFileSize('BlogPhoto'<span style="margin:0px;padding:0px;line-height:1.5;">);
            </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">if</span> (size &gt; 4<span style="margin:0px;padding:0px;line-height:1.5;">) {
                fileSizeBool </span>= <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">true</span><span style="margin:0px;padding:0px;line-height:1.5;">;
                $(</span>"#warning").html("上传文件已经超过4兆!"<span style="margin:0px;padding:0px;line-height:1.5;">);
            } </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">else</span><span style="margin:0px;padding:0px;line-height:1.5;"> {
                fileSizeBool </span>= <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">false</span><span style="margin:0px;padding:0px;line-height:1.5;">;
            }
        }
    });</span></pre><div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"><span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><a title="复制代码" style="margin:0px;padding:0px;color:rgb(45,161,45);border:none;" target="_blank"><img src="https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:920px;border:none;"></a></span></div></div><p style="margin:10px auto;color:rgb(17,17,17);font-family:'Helvetica Neue', Arial;font-size:13px;"><span style="margin:0px;padding:0px;color:rgb(128,128,0);">(5)当点击提交按钮时对其进行文件进行验证:</span></p><div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"><div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"><span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><a title="复制代码" style="margin:0px;padding:0px;color:rgb(45,161,45);border:none;" target="_blank"><img src="https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:920px;border:none;"></a></span></div><pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';">        $("#uploadFileSub").submit(<span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">function</span><span style="margin:0px;padding:0px;line-height:1.5;"> () {
        $(</span>"input[type='text']").each(<span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">function</span><span style="margin:0px;padding:0px;line-height:1.5;"> (k, v) {
            </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">if</span><span style="margin:0px;padding:0px;line-height:1.5;"> ($(v).length) {
                $(v).siblings(</span>"span"<span style="margin:0px;padding:0px;line-height:1.5;">).hide();
            }
        });
        </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">if</span> (fileTypeBool ||<span style="margin:0px;padding:0px;line-height:1.5;"> fileSizeBool) {
            </span><span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">return</span> <span style="margin:0px;padding:0px;color:rgb(0,0,255);line-height:1.5;">false</span><span style="margin:0px;padding:0px;line-height:1.5;">;
        }
    });</span></pre><div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"><span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><a title="复制代码" style="margin:0px;padding:0px;color:rgb(45,161,45);border:none;" target="_blank"><img src="https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:920px;border:none;"></a></span></div></div><p style="margin:10px auto;color:rgb(17,17,17);font-family:'Helvetica Neue', Arial;font-size:13px;"><span style="margin:0px;padding:0px;color:rgb(255,0,0);">【注意】上述对于验证不是太完整,但是基本的架子已经给出。</span></p><p style="margin:10px auto;color:rgb(17,17,17);font-family:'Helvetica Neue', Arial;font-size:13px;">接下来我们来完整的演示整个过程。&nbsp;</p><p style="margin:10px auto;color:rgb(17,17,17);font-family:'Helvetica Neue', Arial;font-size:13px;">&nbsp;<img src="https://images2015.cnblogs.com/blog/589642/201604/589642-20160421162953460-1753447605.gif" alt="" style="padding:0px;border:0px;max-width:920px;"></p><p style="margin:10px auto;color:rgb(17,17,17);font-family:'Helvetica Neue', Arial;font-size:13px;">上述我们一直是利用的纯HTML代码,当然也可以利用MVC的扩展方法来进行,如下(最终渲染的还是表单,本质上是一致的,就不做过多探讨了)</p><div class="cnblogs_code" style="margin:5px 0px;padding:5px;background-color:rgb(245,245,245);border:1px solid rgb(204,204,204);color:rgb(0,0,0);font-family:'Courier New';font-size:12px;"><div class="cnblogs_code_toolbar" style="margin:5px 0px 0px;padding:0px;"><span class="cnblogs_code_copy" style="margin:0px;padding:0px 5px 0px 0px;line-height:1.5;"><a title="复制代码" style="margin:0px;padding:0px;color:rgb(45,161,45);border:none;" target="_blank"><img src="https://i-blog.csdnimg.cn/blog_migrate/48304ba5e6f9fe08f3fa1abda7d326ab.gif" alt="复制代码" style="margin-bottom:0px;padding:0px;max-width:920px;border:none;"></a></span></div><pre style="margin-bottom:0px;padding-right:0px;padding-left:0px;white-space:pre-wrap;font-family:'Courier New';"><span style="margin:0px;padding:0px;line-height:1.5;">@using (Html.BeginForm("UploadFile", "Home", FormMethod.Post, new { enctype = "multipart/form-data" }))

{

<input type=“file” id=“file” name=“file” />
<input type=“submit” value=“提交” />
}

复制代码

结语

这一节我们比较详细的讲述了在MVC中如何进行文件的上传,但是我们还有一点未曾讲到,则是利用流来将如我们上述的图片转换成字节来插入到数据库中。有关上传可以参考园友Insus.NET对此利用流来上传。


原文地址:https://www.cnblogs.com/CreateMyself/p/5414200.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值