将FormData传递到WebAPI的角度.变空 [英] Angular Passing FormData to WebAPI. Getting null

查看:89
本文介绍了将FormData传递到WebAPI的角度.变空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将FormData对象从Angular 7传递到WebAPI,但得到一个空

I am passing FormData object from Angular 7 to WebAPI but I getting a null

感谢您的帮助.谢谢

从打字稿中,我有

selectFile(event){ 
      if (event.target.files[0]) {
          this.blob = event.target.files[0];
      }
    }

save() {

let formDataDTO = new FormData();
        formDataDTO.append('file', this.blob, this.fileName);

this.http.post<T>(this.url, JSON.stringify(formDataDTO), this.getHeaders())
      .pipe(
      catchError(error => {

      }))

}

在WebAPI中,

[HttpPost]
        [Route("file/add")]

        public HttpResponseMessage Save([FromBody] HttpPostedFileBase form)
        {
            var test = form; // form is always null
//cannot access Request.Content.file
// Request.Content.isFormData() is false
// Request.Content.IsMimeMultipartContent() cannot be evaluated

        }

推荐答案

尝试一下:

首先,定义一个发送文件的服务:

first of all, define a service to send the files :

@Injectable()
export class UploadFileSimpleService {

  private baseUrl = "api/SimpleUpload";

  constructor(private http: Http) {}

  private extractData(res: Response) {
    const body = res.json();
    return body || {};
  }

  private handleError(error: Response): Observable<any> {
    console.error("observable error: ", error);
    return Observable.throw(error.statusText);
  }

  postTicket(filesList: FileList): Observable<any> {
    if (!filesList || filesList.length === 0) {
      return Observable.throw("Please select a file.");
    }

    const formData: FormData = new FormData();

    for (let i = 0; i < filesList.length; i++) {
      formData.append(filesList[i].name, filesList[i]);
    }

    const headers = new Headers();
    headers.append("Accept", "application/json");
    const options = new RequestOptions({ headers: headers });

    return this.http
      .post(`${this.baseUrl}/SaveTicket`, formData, options)
      .map(this.extractData)
      .catch(this.handleError);
  }
}

然后创建一个表单以选择文件:

then create a form to select the files :

<form #form="ngForm" (submit)="submitForm(form)" novalidate>

    <div class="form-group">
        <label class="control-label">Screenshot(s)</label>
        <input #screenshotInput required type="file" multiple (change)="fileChange($event)" class="form-control" name="screenshot">
    </div>

    <button class="btn btn-primary" [disabled]="form.invalid" type="submit">Ok</button>
</form>

在此处,提交表单后,调用UploadFileSimpleService发送文件:

here, call the UploadFileSimpleService to send the files after submitting the form :

   @ViewChild("screenshotInput") screenshotInput: ElementRef;

   constructor(private uploadService: UploadFileSimpleService  ) {}

   submitForm(form: NgForm) {
     const fileInput: HTMLInputElement = this.screenshotInput.nativeElement;
     console.log("fileInput.files", fileInput.files);

     this.uploadService
       .postTicket(fileInput.files)
       .subscribe(data => {
         console.log("success: ", data);
       });
   }

,最后在您的MVC Controller中:

and finally in your MVC Controller :

    private readonly IHostingEnvironment _environment;
    public SimpleUploadController(IHostingEnvironment environment)
    {
        _environment = environment;
    }

    [HttpPost("[action]")]
    public async Task<IActionResult> SaveTicket()
    {
        //TODO: save the ticket ... get id
        var uploadsRootFolder = Path.Combine(_environment.WebRootPath, "uploads");
        if (!Directory.Exists(uploadsRootFolder))
        {
            Directory.CreateDirectory(uploadsRootFolder);
        }

        var files = Request.Form.Files;
        foreach (var file in files)
        {
            //TODO: do security checks ...!

            if (file == null || file.Length == 0)
            {
                continue;
            }

            var filePath = Path.Combine(uploadsRootFolder, file.FileName);
            using (var fileStream = new FileStream(filePath, FileMode.Create))
            {
                await file.CopyToAsync(fileStream).ConfigureAwait(false);
            }
        }

        return Created("");
    }

这篇关于将FormData传递到WebAPI的角度.变空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆