微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用asp.net core 2.0中的angular 5进行文件上传.文件为空

我,我正在尝试使用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});
    }

这是发送到服务器之前的数据.

picture sending the data

但是在控制器中,文件的值为null.

null value in the file interface

一个属性绑定到控制器参数,只有文件没有绑定.

任何人都可以告诉我我在哪里,做错了.
 参考文献 – 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);
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐