私は私の見解でこのフォームを持っています:
<!-- Bug (extra 'i') right here-----------v -->
<!-- was: <form method="post" enctype="mulitipart/form-data" action="/Task/SaveFile"> -->
<form method="post" enctype="multipart/form-data" action="/Task/SaveFile">
<input type="file" id="FileBlob" name="FileBlob"/>
<input type="submit" value="Save"/>
<input type="button" value="Cancel" onclick="window.location.href='/'" />
</form>
そして、私のコントローラーのこのコード:
public ActionResult SaveFile( FormCollection forms )
{
bool errors = false;
//this field is never empty, it contains the selected filename
if ( string.IsNullOrEmpty( forms["FileBlob"] ) )
{
errors = true;
ModelState.AddModelError( "FileBlob", "Please upload a file" );
}
else
{
string sFileName = forms["FileBlob"];
var file = Request.Files["FileBlob"];
//'file' is always null, and Request.Files.Count is always 0 ???
if ( file != null )
{
byte[] buf = new byte[file.ContentLength];
file.InputStream.Read( buf, 0, file.ContentLength );
//do stuff with the bytes
}
else
{
errors = true;
ModelState.AddModelError( "FileBlob", "Please upload a file" );
}
}
if ( errors )
{
return ShowTheFormAgainResult();
}
else
{
return View();
}
}
私が見つけたすべてのコードサンプルに基づいて、これはそれを行う方法のようです. 小さいファイルと大きいファイルで試しましたが、結果に違いはありませんでした。フォーム フィールドには、選択したものと一致するファイル名が常に含まれており、Request.Files コレクションは常に空です。
関係ないと思いますが、VS Development Web Server を使っています。私の知る限り、IIS と同じようにファイルのアップロードをサポートしています。
遅くなりつつあり、明らかな何かを見落としている可能性があります。アドバイスをいただければ幸いです。