我的服务器使用 .Net Core 2.1.402
这是我的 C# 类:
public class SampleDetailsDto
{
public Guid Id{ get; set; }
public string Text { get; set; }
public IEnumerable<string> ImageUrls { get; set; }
public IFormCollection Images { get; set; }
}
这是我的 C# 控制器
[HttpPut]
[Route("{id:guid}")]
public async Task<IActionResult> UpdateAsync([FromRoute]Guid id, [FromForm] SampleDetailsDtodto)
{
Console.WriteLine(dto.Text);
Console.WriteLine(dto.Images.Length);
return OK();
}
我使用nswag生成客户端服务,但目前上传多个文件存在错误(https://github.com/RSuter/NSwag/issues/1421#issuecomment-424480418),所以我扩展了更新方法来创建我的,这里是代码:
public update(id: string, dto: SampleDetailsDto | null | undefined): Observable<SampleDetailsDto | null> {
let url_ = this._baseUrl + "/api/v1/Sample/{id}";
if (id === undefined || id === null)
throw new Error("The parameter 'id' must be defined.");
url_ = url_.replace("{id}", encodeURIComponent("" + id));
url_ = url_.replace(/[?&]$/, "");
let options_: any = {
observe: "response",
responseType: "blob",
headers: new HttpHeaders({
"Accept": "application/json"
})
};
return _observableFrom(this.transformOptions(options_)).pipe(_observableMergeMap(transformedOptions_ => {
return this._http.put<SampleDetailsDto>(url_,dto, transformedOptions_);
})).pipe(_observableMergeMap((response_: any) => {
return this.transformResult(url_, response_, (r) => this.processUpdate(<any>r));
})).pipe(_observableCatch((response_: any) => {
if (response_ instanceof HttpResponseBase) {
try {
return this.transformResult(url_, response_, (r) => this.processUpdate(<any>r));
} catch (e) {
return <Observable<SampleDetailsDto | null>><any>_observableThrow(e);
}
} else
return <Observable<SampleDetailsDto | null>><any>_observableThrow(response_);
}));
}
我想同时上传多个文件和数据,在这种情况下,所有图像都链接到 SampleDetailsDto。但是我们也可以想象这种情况也有这种情况:
public class SampleDetailsDto
{
public Guid Id{ get; set; }
public string Text { get; set; }
public IEnumerable<ChildSampleDetailsDto> Children{ get; set; }
}
public class ChildSampleDetailsDto
{
public Guid Id{ get; set; }
public string Text { get; set; }
public IEnumerable<string> ImageUrls { get; set; }
public IFormCollection Images { get; set; }
}
是否可以将数据 + 多个文件发送到 .net Core Web Api?
谢谢