c# - Angular 5 和 ASP.NET Core 2.0 Web API 文件上传 - 文件损坏
问题描述
我正在尝试从我的 Angular 5 客户端应用程序上传 ASP.NET Core 类库文件 (DLL)。在服务器端,我有一个 ASP.NET Core 2.0 Web API 来接收上传的文件。我使用了标准推荐机制。客户端代码如下
uploadAssemblyFile(file: File): any {
const formdata: FormData = new FormData();
formdata.append('file', file, file.name);
const endpoint = '/api/upload';
const req = new HttpRequest('POST', endpoint, formdata, {
reportProgress: true,
responseType: 'json'
});
return this.http.request(req);
}
服务器端代码如下,
[HttpPost()]
[Route("api/upload")]
public async Task<IActionResult> UploadAssembly(IFormFile file)
{
long size = file.Length;
var uploads = Path.Combine(Directory.GetCurrentDirectory(), "uploads");
if (file != null && file.Length > 0)
{
using (var fileStream = new FileStream(Path.Combine(uploads, file.FileName), FileMode.Create))
{
await file.CopyToAsync(fileStream);
}
}
return Ok();
}
Web API 被正确调用,IFormFile 具有所有必需的详细信息。文件被保存。但是,该文件似乎已损坏。使用 Windows 上的属性看到的原始文件和上传文件的大小不同。
PNG文件也是如此。但是,TXT 文件似乎可以正确上传。
有没有人遇到过类似的情况。我看了几个帖子,但没有得到必要的答案。
任何指针肯定会有所帮助。提前致谢
解决方案
推荐阅读
- excel - 下标超出范围/列表对象存在但未找到
- ansible - Ansible 中是否有等效的 grep 和 cut 命令而不使用命令模块?
- excel - VBA用于根据同一行中的另一个单元格值过滤一行中的单元格
- kotlin - 如何在 TornadoFX 中打开多个编辑器视图
- jquery - 无法将大对象从 AJAX 发送到控制器
- php - jenkins 对象实例化错误中的自动化测试使用 Laravel phpunit
- android - 无法从共享首选项中获取数据
- python - ValueError:传递值的形状为 (39, 1),索引暗示 (39, 7)
- java - 无法在 Spring Boot 和 PostgreSQL 之间配置 SSL 连接
- c# - C# 中的 ApiController 未读取变量