简体   繁体   English

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

[英]File Upload using angular 5 in asp.net core 2.0. File is null

I, am trying to upload the file using angular 5 in asp.net core 2.0. 我,我正在尝试使用asp.net core 2.0中的angular 5上传文件。

Here is my server side code. 这是我的服务器端代码。

 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; }
    }

Here us the controller method. 这里是控制器方法。

[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);
        }

Type Script class 键入Script类

export class Data {
    QuestionText: string = "";
    FileUpload: File;
}

Here is the http call. 这是http电话。 The call is invoke to the controller method . 调用调用控制器方法。

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});
    }

Here is the data before sending to the server. 这是发送到服务器之前的数据。

图片发送数据

But in the controller the file is value in null. 但是在控制器中,文件的值为null。

文件界面中的null值

The other property are binded to the controller parameter only file is not binded. 另一个属性绑定到控制器参数,只有文件没有绑定。

Can anyone please tell me where I, am doing wrong. 任何人都可以告诉我我在哪里,做错了。 references - ASP.NET Core 2.0 and Angular 4.3 File Upload with progress 参考资料 - ASP.NET Core 2.0和Angular 4.3文件上传进度

You need to send file like multipart/form-data. 您需要发送类似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);
}

Frontend 前端

private headers: { "Content-Type": "multipart/form-data", boundary: "enterhereurboundary" };

this.uploadFileToUrl = function(file, uploadUrl){
    var fd = new FormData();
    fd.append('usersFile', file);
    $http.post(uploadUrl, fd, {
        headers: this.headers 
    }) 
    .success(function(){ 
    }) 
    .error(function(){ 
    }); 
} 

Backend 后端

    // POST: api/...
    [HttpPost]
    public async Task<ActionResult<string>> UploadUsersAsync(IFormFile usersFile)
    {
        // do something here...
    }

Also, notice that the name of the file u send in the frontend has to be the same as the IFormFile variable name you use in the backend... 另请注意,前端发送的文件名必须与后端使用的IFormFile变量名相同...

Related links 相关链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM