![](/img/trans.png)
[英]asp.net core web api how to send json object from client side c#
[英]unable to send IFormFile to ASP.Net Core Web API using C# client
我有一个 ASP.Net Core Web API,控制器 POST 方法定义如下:
[HttpPost("SubmitFile")]
public async Task<IActionResult> SubmitFile(IFormFile file)
{
}
我有一个客户端方法来调用 API SubmitFile() 方法,定义如下:
[HttpPost]
public async Task<IActionResult> Index(ICollection<IFormFile> files)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(_options.SiteSpecificUrl);
foreach (var file in files)
{
if (file.Length <= 0)
continue;
var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
var fileContent = new StreamContent(file.OpenReadStream());
fileContent.Headers.Add("X-FileName", fileName);
fileContent.Headers.Add("X-ContentType", file.ContentType);
var response = await client.PostAsync(_options.WebApiPortionOfUrl, fileContent);
}
}
return View();
}
当执行客户端发送时,在服务器端,SubmitFile() 中的断点显示文件参数为空。 如何正确发送文件? 保留服务器端 API 很重要,因为我让 Swashbuckle/Swagger 正确生成可以发送文件的 UI。
我找到了几种方法来做到这一点。 这里是最简单的。 请注意,这是一个 ASP.Net Core 客户端解决方案:
[HttpPost]
public async Task<IActionResult> Index(ICollection<IFormFile> files)
{
using (var client = new HttpClient())
{
client.BaseAddress = new Uri(_options.SiteSpecificUrl);
foreach (var file in files)
{
if (file.Length <= 0)
continue;
var fileName = ContentDispositionHeaderValue.Parse(file.ContentDisposition).FileName.Trim('"');
using (var content = new MultipartFormDataContent())
{
content.Add(new StreamContent(file.OpenReadStream())
{
Headers =
{
ContentLength = file.Length,
ContentType = new MediaTypeHeaderValue(file.ContentType)
}
}, "File", fileName);
var response = await client.PostAsync(_options.WebApiPortionOfUrl, content);
}
}
}
}
这个控制器方法是从 .cshtml 页面调用的,如下所示:
@{
ViewData["Title"] = "Home Page";
}
<form method="post" asp-action="Index" asp-controller="Home" enctype="multipart/form-data">
<input type="file" name="files" multiple />
<input type="submit" value="Upload" />
</form>
此窗体显示两个按钮,“选择文件”,它显示一个“选择文件”对话框,以及“上传”,它调用 HomeController.Index 方法。
这对我有用:
前端:
HTML/CSS:
<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>
JavaScript:
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.");
}
}
后端:
Startup.cs: services.AddAntiforgery(o => o.HeaderName = "XSRF-TOKEN");
控制器:
public ActionResult OnPostUpload(List<IFormFile> files)
{
try
{
var file = files.FirstOrDefault();
var inputstream = file.OpenReadStream();
XSSFWorkbook workbook = new XSSFWorkbook(stream);
var FIRST_ROW_NUMBER = {{firstRowWithValue}};
ISheet sheet = workbook.GetSheetAt(0);
// Example: var firstCellRow = (int)sheet.GetRow(0).GetCell(0).NumericCellValue;
for (int rowIdx = 2; rowIdx <= sheet.LastRowNum; rowIdx++)
{
IRow currentRow = sheet.GetRow(rowIdx);
if (currentRow == null || currentRow.Cells == null || currentRow.Cells.Count() < FIRST_ROW_NUMBER) break;
var df = new DataFormatter();
for (int cellNumber = {{firstCellWithValue}}; cellNumber < {{lastCellWithValue}}; cellNumber++)
{
//business logic & saving data to DB
}
}
}
catch(Exception ex)
{
throw new FileFormatException($"Error on file processing - {ex.Message}");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.