我有一个ASP.Net核心Web API,控制器POST方法定义如下:
ConstraintSet set = new ConstraintSet();
set.setVisibility(checkIcon.getId(), INVISIBLE);
set.applyTo(container);
我有一个客户端方法来调用API SubmitFile()方法,定义如下:
[HttpPost("SubmitFile")]
public async Task<IActionResult> SubmitFile(IFormFile file)
{
}
执行客户端发送时,在服务器端,SubmitFile()中的断点显示file参数为null。我怎样才能正确发送文件?保留服务器端API很重要,因为我让Swashbuckle / Swagger正确生成可以发送文件的UI。
答案 0 :(得分:4)
我发现了几种方法。这是最简单的。请注意,这是一个ASP.Net Core客户端解决方案:
outer apply
此控制器方法从.cshtml页面调用,如下所示:
select b.*, i.num_invoices, i.price, d.numdownloads
from book b outer apply
(select count(*) as num_invoices, sum(i.price) as price
from invoice i
where i.BookId = b.Id and i.State = 'successful'
) i outer apply
(select count(*) as numdownloads
from download d
where d.BookId = b.Id and d.State = 'successful'
) d;
此表单显示两个按钮,&#34;选择文件&#34;,它会显示一个&#34;选择文件&#34;对话框和&#34;上传&#34;,它调用HomeController.Index方法。
答案 1 :(得分:0)
这对我有用:
前端:
HTML / CSS:
<Grid>
<CommandBar VerticalAlignment="Bottom">
<AppBarToggleButton x:Name="toggleBtn" Icon="UnFavorite" Label="UnFavorite"/>
</CommandBar>
<VisualStateManager.VisualStateGroups>
<VisualStateGroup>
<VisualState x:Name="checkedbtn">
<VisualState.StateTriggers>
<StateTrigger IsActive="{Binding ElementName=toggleBtn, Path=IsChecked}"/>
</VisualState.StateTriggers>
<VisualState.Setters>
<Setter Target="toggleBtn.Icon" Value="Favorite"/>
<Setter Target="toggleBtn.Label" Value="Favorite"/>
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</Grid>
JavaScript:
<div id="" class="col-xs-12 info-box">
<div class="col-xs-12">
<a role="button" data-toggle="collapse" href="#upload-sample" aria-expanded="false">
<h3><span class="glyphicon glyphicon-upload"></span> Upload de Arquivo</h3>
</a>
</div>
<div id="upload-sample" class="col-xs-12 collapse">
<form method="post" enctype="multipart/form-data">
<div>
<div class="form-group attach" style="width: 100%;">
<label>Select Excel File <button type="button" id="btnDownloadTemplate">(Download Template)</button></label>
<div class="col-md-12"><input type="file" id="fUpload" name="files" multiple class="form-control" style="max-width: 400px;" /></div>
</div>
<div class="filter-button" style="width: 100%;">
<button onclick="AJAXSubmit(this); return false;" id="btnUpload" class="btn btn-primary">Send File and update data</button>
</div>
</div>
</form>
</div>
后端:
Startup.cs:async function AJAXSubmit(oFormElement) {
const files = $('#fUpload').prop("files");
const fdata = new FormData();
for (var i = 0; i < files.length; i++) {
fdata.append("files", files[i]);
}
if (files.length > 0) {
Block();
$.ajax({
type: "POST",
url: "/{{controllerName}}/OnPostUpload?handler=Upload",
beforeSend: function (xhr) {
xhr.setRequestHeader("XSRF-TOKEN",
$('input:hidden[name="__RequestVerificationToken"]').val());
},
data: fdata,
contentType: false,
processData: false,
success: function (response) {
document.getElementById("fUpload").value = "";
//Unblock();
//toastr.success(response, "File processed successfully");
FilterHandlebarsF.Search(this, true);
},
error: function (response) {
document.getElementById("fUpload").value = "";
//Unblock();
//toastr.warning(response, "Error on processing");
}
});
}
else {
//toastr.warning("Please, select a file.");
}
}
控制器:
services.AddAntiforgery(o => o.HeaderName = "XSRF-TOKEN");