在Angular2 + .NET中通过POST发送文件

时间:2017-06-13 19:56:31

标签: .net angular

我试图通过邮寄发送所选文件。但是,后端的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);
}

0 个答案:

没有答案