【发布时间】:2018-07-31 19:55:09
【问题描述】:
我正在使用 ASP.Net Core 2.1,并尝试在返回文件的 url 时上传文件,而不刷新页面。
我正在尝试在 site.js 中编写 JavaScript,因为 _RenderPartial("scripts") 在页面末尾呈现所有脚本,因此在剃刀视图中直接使用脚本标记不起作用。其次,将它添加到 site.js 让我有机会跨站点视图调用脚本。
我的控制器动作看起来像:
[HttpPost]
[DisableRequestSizeLimit]
public async Task<IActionResult> Upload()
{
// Read & copy to stream the content of MultiPart-Form
// Return the URL of the uploaded file
return Content(FileName);
}
我的看法是这样的:
<form id="FileUploadForm" action="~/Resources/Upload" method="post" enctype="multipart/form-data">
<input name="uploadfile" type="file" />
<button name="uploadbtn" type="submit" onclick="SubmitForm(this.parentElement, event)">Upload</button>
site.js 目前看起来像:
function SubmitForm(form, caller) {
caller.preventDefault();
$.ajax(
{
type: form.method,
url: form.action,
data: form.serialize(),
success: function (data) { alert(data); },
error: function (data) { alert(data); }
})}
目前,代码会绕过整个脚本并上传文件并返回显示文件名的新视图。我需要帮助来创建 javascript。
【问题讨论】:
-
不,也许您可以添加一个相同的示例。
标签: javascript jquery file-upload asp.net-core asp.net-core-mvc-2.1