我,我正在尝试使用asp.net core 2.0中的angular 5上传文件.
这是我的服务器端代码.
public class QuestionViewModel { public Guid QuestionId { get; set; } public string QuestionText { get; set; } public DateTime CreateDate { get; set; } public string PictureUrl { get; set; } public FormFile FileUpload { get; set; } }
这里是控制器方法.
[HttpPost] [AllowAnonymous] public JsonResult QuestionPhotoPost([FromBody] QuestionViewModel model) { GenericResponseObject<List<QuestionViewModel>> genericResponseObject = new GenericResponseObject<List<QuestionViewModel>>(); genericResponseObject.IsSuccess = false; genericResponseObject.Message = ConstaintStingValue.TagConnectionFailed; List<QuestionViewModel> questionViewModel = new List<QuestionViewModel>(); return Json(genericResponseObject); }
键入Script类
export class Data { QuestionText: string = ""; FileUpload: File; }
这是http电话.调用调用控制器方法.
public QuestionPostHttpCall(_loginVM: QuestionVmData): Observable<QuestionPhotoViewModel> { console.log(_loginVM) const headers = new HttpHeaders().set('Content-Type','application/json; charset=utf-8'); return this._httpClientModule.post<QuestionPhotoViewModel>(this.questionPhoto,_loginVM,{ headers}); }
这是发送到服务器之前的数据.
但是在控制器中,文件的值为null.
另一个属性绑定到控制器参数,只有文件没有绑定.
任何人都可以告诉我我在哪里,做错了.
参考文献 – ASP.NET Core 2.0 and Angular 4.3 File Upload with progress
解决方法
您需要发送类似multipart / form-data的文件.
upload(file: File,questionText: string): Observable<FileResponseModel> { const url: string = Urls.uploadFiles(); const formData: FormData = new FormData(); formData.append('attachment',file,file.name); formData.append('questionText',questionText); const options = { headers: new HttpHeaders().set('enctype','multipart/form-data') }; return this.httpService.post(url,formData,options); }
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。