更新时间:2023-01-17 10:47:14
您不必创建数组缓冲区.只需使用文件输入并发送表单数据即可.
You don't have to create an array buffer. Just use a file-input and send form-data.
假设您使用的是angular 4.3 ++,并使用@angular/common/http
中的 HttpClientModule :
Assuming you are on angular 4.3++ and using HttpClientModule from @angular/common/http
:
角度服务方法
public uploadFile(file: File): Observable<any> {
const formData = new FormData();
formData.Append('myFile', file);
return this.http.post('my-api-url', formData);
}
现在您是asp.net核心终结点
now you asp.net-core endpoint
[HttpPost]
// attention name of formfile must be equal to the key u have used for formdata
public async Task<IActionResult> UploadFileAsync([FromForm] IFormFile myFile)
{
var totalSize = myFile.Length;
var fileBytes = new byte[myFile.Length];
using (var fileStream = myFile.OpenReadStream())
{
var offset = 0;
while (offset < myFile.Length)
{
var chunkSize = totalSize - offset < 8192 ? (int) totalSize - offset : 8192;
offset += await fileStream.ReadAsync(fileBytes, offset, chunkSize);
}
}
// now save the file on the filesystem
StoreFileBytes("mypath", fileBytes);
return Ok();
}