要使用jQuery僅上傳HTML 5,並非所有的瀏覽器都能夠這樣做。
你可以做這樣的事情。
您需要有一個IFRAME用於使用該文件創建帖子。
像這樣:
<iframe id="targetUpload" name="targetUpload"></iframe>
在表單你的目標設定在iframe中。
<form target="targetUpload" name="file" runat="server" method="post" id="file" enctype="multipart/form-data" action="@Url.Content("~/Controller/Upload")">
和一個提交按鈕
<input type="Submit" value="Upload">
這是上傳不刷新頁面文件的方式。
你可以在使用jquery上傳之前對待這些文件。 (文件類型,大小等)
而且在YOUT控制器,你recive文件:
public ActionResult Upload(HttpPostedFileBase file)
{
if (file != null && file.ContentLength > 0)
{
//Save the file in the server path
string savePath = Server.MapPath("~/Upload/");
string fileName = file.FileName;
savePath += fileName;
file.SaveAs(savePath);
}
return RedirectToAction("Index");
}