【发布时间】:2021-07-07 17:38:42
【问题描述】:
我尝试学习 Web 开发,并测试了我是否可以仅使用 Razor 页面/模型的 POST 端为 AJAX 脚本通过另一个 razor 页面上传文件,同时不处理"FileUpload"-page 作为页面。
我将减少代码,只是为了说明我的意思:
索引.cshtml
@* return false, to prevent rendering of FileUpload.cshtml *@
<form asp-page="./FileUpload" enctype="multipart/form-data"
onsubmit="Uploader(this);return false;" method="post">
...
<script>
async function Uploader(element) {
var result = ...
try {
const response = await fetch(element.action, {
method: 'POST',
body: formData
}).then(response => response.json())
.then(data => ...
文件上传.cshtml.cs
// Another code checks the file and adds errors to ModelState
public async Task<IActionResult> OnPostAsync()
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
...
发布后 Index.cshtml 中的示例响应:
{"file":["File type is not allowed or the signature don't match the extenstion"]}
这可行,但我想知道这样做是否可以接受,或者是否会对安全性、性能、是否是一种糟糕/错误的方式等产生任何后果?
感谢您的时间和帮助。
【问题讨论】:
标签: ajax asp.net-core razor-pages