欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

用Html5與Asp.net MVC上傳多個(gè)文件的實(shí)現(xiàn)代碼

 更新時(shí)間:2012年08月30日 11:57:35   作者:  
Html 5 的有一些File API,對(duì)Form表單增強(qiáng)的特性,讓我們輕松支持多文件上傳,看下面的Html片斷代碼

復(fù)制代碼 代碼如下:

<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中,我們可以這么實(shí)現(xiàn):
復(fù)制代碼 代碼如下:

@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" />
}

假設(shè)這是一個(gè)HomeController下View, 即將提交到Upload的Action,看下面服務(wù)端的代碼:
復(fù)制代碼 代碼如下:

[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");
}

好的,就這么簡(jiǎn)單。 這里我們把接收到文件存儲(chǔ)到App_Data文件夾中,然后返回Index的Action. 看下面圖片,我們能夠從文件選擇器選擇多張圖片:
mutliImagesfiles 

關(guān)于HTML5這個(gè)特性在那些瀏覽器支持,您可以去這里查看。 您還可以查看W3C官方的文檔。我們?cè)贔ireFox 14.01下測(cè)試能過(guò)。

希望對(duì)您Web開(kāi)發(fā)有幫助。

相關(guān)文章

最新評(píng)論