我试图通过邮寄发送所选文件。但是,后端的File方法不会被执行。
这是前端代码:
clicked(event: EventTarget) {
let eventObj: MSInputMethodContext = <MSInputMethodContext>event;
let target: HTMLInputElement = <HTMLInputElement>eventObj.target;
let fileCount: number = target.files.length;
let formData = new FormData();
if (fileCount > 0) { // a file was selected
for (let i = 0; i < fileCount; i++) {
formData.append('file[]', target.files.item(i));
}
this.http.post("http://localhost:1234/file", formData);
}
}
这是后端代码:
[HttpPost]
[Route("file")]
[AcceptVerbs("POST")]
public void File(System.Web.HttpPostedFileBase file)
{
string path = "~/UploadedFiles/" + file.FileName;
file.SaveAs(path);
_transactionService.ImportFile(path);
}