如何在 Visual Studio 2017 中将 excel 文件从 angular 4 传递到 C# 控制器?
how to pass excel file to C# controller from angular 4 in Visual Studio 2017?
在我的网络应用程序中,我必须导入一个 excel 文件并想将其传递给服务器端控制器。
在服务器端我使用了EPPlus。
我怎样才能达到同样的效果?
任何人都可以帮助实现同样的目标
在模板中创建文件输入
<input type="file" #fileInput />
<button (click)="uploadFile();">Upload</button>
然后转到您的组件
@ViewChild('fileInput') fileInput;
// inject httpclient from @angular/common/http
...
public uploadFile(): void {
if (this.fileInput.files.length === 0) {
return; // maybe needs more checking
}
const formData = new FormData();
formData.append('file', this.fileInput.files[0]);
this.http.post('http://my-url/api/my-endpoint', formData).subscibre(...); // the usual
}
现在在您的 API 中创建一个这样的端点
[HttpPost]
// the name here must be file for the parameter, bc you declared it as such in your formdata
public IActionResult UploadFile([FromBody] IFormFile file)
{
// depending on what you wanna do you can either create and store it in the filesystem or copy the file into a byte array and store it in your db
}
如果您明确说明文件的保存位置,我可以展开 C# 代码。但一般来说,这就是您在 back-end.
中从 front-end 接收文件的方式
很多关于上传文件或第 3 方 pre-made 组件的类似问题...
还有很多关于接收文件的类似问题和示例server-side...
- asp.net - How to upload file with web-api
- Upload Files In ASP.NET MVC 5
- Etc.
在我的网络应用程序中,我必须导入一个 excel 文件并想将其传递给服务器端控制器。
在服务器端我使用了EPPlus。
我怎样才能达到同样的效果?
任何人都可以帮助实现同样的目标
在模板中创建文件输入
<input type="file" #fileInput />
<button (click)="uploadFile();">Upload</button>
然后转到您的组件
@ViewChild('fileInput') fileInput;
// inject httpclient from @angular/common/http
...
public uploadFile(): void {
if (this.fileInput.files.length === 0) {
return; // maybe needs more checking
}
const formData = new FormData();
formData.append('file', this.fileInput.files[0]);
this.http.post('http://my-url/api/my-endpoint', formData).subscibre(...); // the usual
}
现在在您的 API 中创建一个这样的端点
[HttpPost]
// the name here must be file for the parameter, bc you declared it as such in your formdata
public IActionResult UploadFile([FromBody] IFormFile file)
{
// depending on what you wanna do you can either create and store it in the filesystem or copy the file into a byte array and store it in your db
}
如果您明确说明文件的保存位置,我可以展开 C# 代码。但一般来说,这就是您在 back-end.
中从 front-end 接收文件的方式很多关于上传文件或第 3 方 pre-made 组件的类似问题...
还有很多关于接收文件的类似问题和示例server-side...
- asp.net - How to upload file with web-api
- Upload Files In ASP.NET MVC 5
- Etc.