发布于 2016-06-29 06:34:05 | 178 次阅读 | 评论: 0 | 来源: 网友投递

这里有新鲜出炉的精品教程,程序狗速度看过来!

ASP.NET

ASP.NET 是.NET FrameWork的一部分,是一项微软公司的技术,是一种使嵌入网页中的脚本可由因特网服务器执行的服务器端脚本技术,它可以在通过HTTP请求文档时再在Web服务器上动态创建它们。 指 Active Server Pages(动态服务器页面) ,运行于 IIS(Internet Information Server 服务,是Windows开发的Web服务器)之中的程序 。


Html 5 的有一些File API,对Form表单增强的特性,让我们轻松支持多文件上传,看下面的Html片断代码

 
<form action="/Home/Upload" enctype="multipart/form-data" id="form2" method="post"> 
<input type="file" name="fileToUpload" id="fileToUpload2" multiple="multiple" /> 
<input type="submit" value="submit" /> 
</form> 

那在Asp.net MVC web application中,我们可以这么实现:
 
@using (Html.BeginForm("Upload", "Home", FormMethod.Post, new { enctype = "multipart/form-data", id = "form2" })) 
{ 
<label for="file">Upload Image:</label> 
<input type="file" name="fileToUpload" id="fileToUpload2" multiple="multiple" /> 
<input type="submit" value="Upload Image by submit" /> 
} 

假设这是一个HomeController下View, 即将提交到Upload的Action,看下面服务端的代码:
 
[HttpPost] 
public ActionResult Upload(HttpPostedFileBase[] fileToUpload) 
{ 
foreach (HttpPostedFileBase file in fileToUpload) 
{ 
string path = System.IO.Path.Combine(Server.MapPath("~/App_Data"), System.IO.Path.GetFileName(file.FileName)); 
file.SaveAs(path); 
} 

ViewBag.Message = "File(s) uploaded successfully"; 
return RedirectToAction("Index"); 
} 

好的,就这么简单。 这里我们把接收到文件存储到App_Data文件夹中,然后返回Index的Action. 看下面图片,我们能够从文件选择器选择多张图片:
 

关于HTML5这个特性在那些浏览器支持,您可以去这里查看。 您还可以查看W3C官方的文档。我们在FireFox 14.01下测试能过。

希望对您Web开发有帮助。



最新网友评论  共有(0)条评论 发布评论 返回顶部

Copyright © 2007-2017 PHPERZ.COM All Rights Reserved   冀ICP备14009818号  版权声明  广告服务