コントローラでは、アップロードされたファイルに次のようにアクセスできます。
if(Request.Files.Count > 0 && Request.Files[0].ContentLength > 0) {
HttpPostedFileBase postFile = Request.Files.Get(0);
string filename = GenerateUniqueFileName(postFile.FileName);
postFile.SaveAs(server.MapPath(FileDirectoryPath + filename));
}
protected virtual string GenerateUniqueFileName(string filename) {
// get the extension
string ext = Path.GetExtension(filename);
string newFileName = "";
// generate filename, until it's a unique filename
bool unique = false;
do {
Random r = new Random();
newFileName = Path.GetFileNameWithoutExtension(filename) + "_" + r.Next().ToString() + ext;
unique = !File.Exists(FileDirectoryPath + newFileName);
} while(!unique);
return newFileName;
}
テキストフィールドは、通常どおり、Request.Form[...]などのコントローラアクションに到達します。フォームのenctypeも「multipart/form-data」に設定する必要があることに注意してください。残りの作業を行うには、ASP.NETMVCについて十分に理解しているようです。次のようにaspxビューでフォームタグを宣言できることにも注意してください。ただし、必要に応じて、より従来のアプローチを使用することもできます。
<% using(Html.BeginForm<FooController>(c => c.Submit(), FormMethod.Post, new { enctype = "multipart/form-data", @id = formId, @class = "submitItem" })) { %>
<% } %>