C# 无法将文件从Angular 8上载到.net core 2.2?
角度HTML:C# 无法将文件从Angular 8上载到.net core 2.2?,c#,asp.net,.net,angular,file,C#,Asp.net,.net,Angular,File,角度HTML: <input #file type="file" name="file" id="file" (click)="handleFileInput($event.target.files)" /> <input type="button" value="Save" (click) ="UploadForm()" /> .Net核心Web API代码: public async Task<SingleResponseModel<Dicti
<input #file type="file" name="file" id="file" (click)="handleFileInput($event.target.files)" />
<input type="button" value="Save" (click) ="UploadForm()" />
.Net核心Web API代码:
public async Task<SingleResponseModel<Dictionary<string, int>>> FileUploadAsync([FromForm] IFormFile file)
{
string createdby = "abcd";
SingleResponseModel<Dictionary<string, int>> singleResponseModel = new SingleResponseModel<Dictionary<string, int>>();
FileUploadDocumentModel fileUploadDocumentModel = null;
string pathToSave = string.Empty;
string fullPath = string.Empty;
try
{
pathToSave = Path.Combine(Directory.GetCurrentDirectory(), "Documents");
if (file != null)
{
fullPath = Path.Combine(pathToSave, file.FileName);
using (var stream = new FileStream(fullPath, FileMode.Create))
{
file.CopyTo(stream);
}
}
}
catch(Exception ex)
{
throw ex;
}
}
public async Task FileUploadAsync([FromForm]ifformfile)
{
字符串createdby=“abcd”;
SingleResponseModel SingleResponseModel=新的SingleResponseModel();
FileUploadDocumentModel FileUploadDocumentModel=null;
string pathToSave=string.Empty;
string fullPath=string.Empty;
尝试
{
pathToSave=Path.Combine(Directory.GetCurrentDirectory(),“Documents”);
如果(文件!=null)
{
fullPath=Path.Combine(pathToSave,file.FileName);
使用(var stream=newfilestream(fullPath,FileMode.Create))
{
file.CopyTo(流);
}
}
}
捕获(例外情况除外)
{
掷骰子;
}
}
上传文件时,我收到[FromForm]FormFile对象null。请告诉我如何解决此问题?看起来您没有向组件发送文件。所以试着这样写:
<input #file type="file" name="file" id="file" (click)="handleFileInput(file.files)" />
实际服务方式:
和FileDto.cs
:
看起来您没有向组件发送文件。所以试着这样写:
<input #file type="file" name="file" id="file" (click)="handleFileInput(file.files)" />
实际服务方式:
和FileDto.cs
:
<input type="file" #file placeholder="Choose file" (change)="uploadFile(file.files)"
accept="image/x-png,image/gif,image/jpeg" multiple>
public uploadFile = (files) => {
if (files.length === 0) {
return;
}
const fileToUpload = files[0] as File;
const formData = new FormData();
formData.append('file', fileToUpload, fileToUpload.name);
this.uploadService.uploadFile(formData)
.subscribe(event => {
if (event.type === HttpEventType.UploadProgress) {
this.progress = Math.round(100 * event.loaded / event.total);
} else if (event.type === HttpEventType.Response) {
this.message = 'Upload success.';
this.onUploadFinished.emit({ dbPath: event.body.dbPath});
}
});
}
uploadFile(file: FormData): Observable<any> {
return this.http.post(`${environment.baseApi}Upload`,
file, {reportProgress: true, observe: 'events'})
.pipe(map(v => v));
}
[HttpPost]
public IActionResult Upload([FromForm] FileDto fileDto)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (fileDto.File != null)
{
return Ok(new { dbPath = _uploadFileService.UploadFile(fileDto.File ) });
}
return BadRequest();
}
using Microsoft.AspNetCore.Http;
using System;
using System.Collections.Generic;
using System.Text;
namespace eshop.Persistence.Core.Dtos
{
public class FileDto
{
public IFormFile File { get; set; }
public string FolderPath { get; set; }
public string FileName { get; set; }
}
}