作者:towardsnext 在MVC应用中,文件的上传处理比较简单,下面是一个结合JQuery的例子。 然后,我们需要根据BeginForm中FileController和action(Upload)在指定的Controller中处理请求,参考如下代码:
(全文完)<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<h2>Files uploaded to server</h2>
<div id="dialog" title="Upload files">
<% using (Html.BeginForm("Upload", "File", FormMethod.Post, new { enctype = "multipart/form-data" }))
{%><br />
<p><input type="file" id="fileUpload" name="fileUpload" size="23"/> ;</p><br />
<p><input type="submit" value="Upload file" /></p>
<% } %>
</div>
<a href="#" onclick="jQuery('#dialog').dialog('open'); return false">Upload File</a>
</asp:content>
public void Upload(
{
foreach (string inputTagName in Request.Files)
{
HttpPostedFileBase file = Request.Files[inputTagName];
if (file.ContentLength > 0)
{
string filePath = Path.Combine(HttpContext.Server.MapPath("../Uploads")
, Path.GetFileName(file.FileName));
file.SaveAs(filePath);
}
}
RedirectToAction("Index", "File");
}
>> 本文固定链接: http://www.vcgood.com/archives/3441