asp.net core webappでローカルファイルをアップロードするには、一般的なHTMLと同様、formタグにenctype=”multipart/form-data”を付け、<input type=”file”>を使用して、ファイル本体をアップロードする。
<form enctype="multipart/form-data" method="post">
・・・
アップロードファイル:<input type="file" name="fupFile"/>
<button class="btn btn-primary" type="submit">アップロード</button>
</form>受取側は、IFormFileクラスを指定して、引数として受取ることが可能。
public async Task OnPost([FromForm]IFormFile fupFile) {
・・・
Stream stm = fupFile.OpenReadStream();
// ファイルに対する処理
・・・
}まだ試していないが、Blazorの場合は少々異なり、<InputFile>タグとそのイベントハンドラで処理ができるようだ。
・・・
<InputFile OnChange="@OnInputFileChange"/>
・・・
@code {
・・・
protected void OnInputFileChange(InputFileChangeEventArgs e) {
Stream stm = e.File.OpenReadStream();
// ファイル処理
}
}ちなみに、webapp中のハンドラで動的に作成したファイルのダウンロードだが、ASP.NETで使っていた方法では、ミドルウェアのせいで実行時エラー(Responseヘッダは上書き禁止)が出て、動作しなかった。
色々試してみて、結局MVC(FileContentResultを返す)を一部使用することによってダウンロードできるようになったが、他に方法はないものだろうか・・・
@{
Dictionary<string,string> prm = new Dictionary<string,string>() {
{"Query" : Model.Query}
};
}
・・・
<button class="btn btn-primary" asp-controller="[コントローラ]" asp-action="Download" asp-asp-all-route-data="prm">ダウンロード</button>public async Task<FileContentResult> Download(string Query) {
・・・
MemoryStream stm = new MemoryStream();
// MemoryStreamにファイルを作成する処理
・・・
byte[] buff = stm.ToArray();
return new FileContentResult(
buff,
"application/octet-stream"
) {
FileDownloadName = filename
};
}










Users Today : 0
Users Yesterday : 36
Users Last 7 days : 198
Users Last 30 days : 864
Users This Month : 69
Users This Year : 3417
Total Users : 97932
Views Today :
Views Yesterday : 46
Views Last 7 days : 240
Views Last 30 days : 1107
Views This Month : 84
Views This Year : 4141
Total views : 136587
Who's Online : 0