优点:ajax可以传好几个参数(当然以json形式),传100个都无所谓。你要是用<a href=“网址?参数=值”></a>的方法传100得写死。。。(公司需求,至少要传100多个参数)
因为你只是单纯的放在目录文件夹下没有用的,我们平时在服务器某目录下添加某一个文件都是右键,添加XXX项这样,这样才能真正的将文件放在服务器中。
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;">在 <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> 视图中添加上如下:</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;"><</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;">></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(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;">/><</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;">/></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(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;">/></span>
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 ><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兆,在配置文件中我们知道 <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 = <span style="margin:0px;padding:0px;color:rgb(128,0,128);line-height:1.5;">4096</span></span> 默认是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';"> <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> /></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兆,于是乎继续在配置文件中去修改文件大小,但是结果还是一样。我们继续仔细看看该结果的提示,根据提示去找到配置文件下的节点再试试,我们在 <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> 节点下设置为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';"> <security>
<requestFiltering>
<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>>
</requestLimits>
</requestFiltering>
</security></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 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> 中设置不行,但是有些这样设置是正确的,这是什么原因?最终找到了答案:</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';"><system.web>
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 > 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;">接下来我们来完整的演示整个过程。 </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-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" }))