Наконец-то получил его на работу! Я попробовал все предложения здесь, но ни один не работал для меня. Я вернулся к использованию XMLHttpRequest. Все, что мне нужно было сделать, это удалить строку setRequestHeader и вуаля - все заработало.
Вот машинопись:
export class FileService {
_message: string;
private url = environment.fileUploadX_Url;//this.config.FILEUPLOAD_ENDPOINT;//
@Output('fileLoad') fileLoaded: EventEmitter<string> = new EventEmitter<string>();
constructor(private http: HttpClient, private config: ConfigService) {
}
upload(file: File): void {
var xhr = this.createCORSRequest('POST', this.url);
const fd = new FormData();
fd.append('file', file);
xhr.send(fd);
}
createCORSRequest(method, url) {
var xhr = new XMLHttpRequest();
xhr.withCredentials = false;
xhr.addEventListener('loadend', this.handleLoadEnd.bind(this));
xhr.addEventListener('error', this.handleLoadEnd);
xhr.open(method, url, true);
// xhr.setRequestHeader("Content-Type", "multipart/form-data");
return xhr;
}
Вот контроллер в веб-API:
[Produces("application/json")]
[Route("api/[controller]")]
[ApiController]
public class UploadFileController : ControllerBase
{
[DisableRequestSizeLimit]
[HttpPost("UploadFile")]
public async Task<ActionResult> UploadFile()
{
try
{
var file = Request.Form.Files[0];
string fileName = file.FileName;
var filePath = Path.Combine("/Logs", fileName);
using (var stream = new FileStream(filePath, FileMode.Create))
{
await file.CopyToAsync(stream);
}
}
catch (Exception ex) {
return BadRequest("Unable to upload the file." );
}
return Ok();
}
}
Вот файл startup.cs в веб-интерфейсе:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddCors(o => o.AddPolicy("ThePolicy", builder =>
{
builder.AllowAnyOrigin()
.AllowAnyMethod()
.AllowAnyHeader();
}));
//services.AddCors(c =>
//{
// c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin());
//});
ConfigureServicesModule<AviationIQ_Dev_Phase2Context>.Register(services, Configuration);
services.Configure<FormOptions>(o => {
o.ValueLengthLimit = int.MaxValue;
o.MultipartBodyLengthLimit = int.MaxValue;
o.MemoryBufferThreshold = int.MaxValue;
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
//app.UseHttpsRedirection();
//app.UseMvc();
ConfigureModule.Configure(app, env, Configuration);
app.UseCors("ThePolicy");
}
}