加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > asp.Net > 正文

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

发布时间:2020-12-16 07:42:29 所属栏目:asp.Net 来源:网络整理
导读:我,我正在尝试使用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 st
我,我正在尝试使用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);
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读