复制代码 代码如下:
<asp:ContentID="Content2"ContentPlaceHolderID="MainContent"runat="server">
<h2>Filesuploadedtoserver</h2>
<divid="dialog"title="Uploadfiles">
<%using(Html.BeginForm("Upload","File",FormMethod.Post,new
{
enctype="multipart/form-data"
}
))
{%>
<br/>
<p><inputtype="file"id="fileUpload"name="fileUpload"size="23"/>;</p><br/>
<p><inputtype="submit"value="Uploadfile"/></p>
<%}%>
</div>
<ahref="#"onclick="jQuery('#dialog').dialog('open');returnfalse">UploadFile</a>
</asp:content>
然后,我们需要根据BeginForm中FileController和action(Upload)在指定的Controller中处理请求,参考如下代码:
复制代码 代码如下:
publicvoidUpload(
{
foreach(stringinputTagNameinRequest.Files)
{
HttpPostedFileBasefile=Request.Files[inputTagName];
if(file.ContentLength>0)
{
stringfilePath=Path.Combine(HttpContext.Server.MapPath("../Uploads")
,Path.GetFileName(file.FileName));
file.SaveAs(filePath);
}
}
RedirectToAction("Index","File");
}