locked
File posted from angular application not received at C# WebAPI code. RRS feed

  • Question

  • User-137472927 posted

    Hi All,

    I have an angular application which has file upload functionality. This angular application uses Post() method of HTTPClient to send file(as FormData object) to C# WebAPI code: 
     
    Angular/Typescript code- 

    if (this.fileList && this.fileList.length > 0) {
        let file: File = this.fileList[0];
        const formData: FormData = new FormData();
        formData.append("fileKey", file, file.name);
        let apiURL: string = "http://MyProjectSampleAPI.com/api/UploadFile"; 
        let httpHeaders = new HttpHeaders();
        let options= {
            headers: httpHeaders,
            withCredentials: true
        };
        return this.httpClient.post(apiURL, formData).map((response) => response)
                .catch((err) => this.handleException(err));

    C# WebAPI Controller Method code- 
    [HttpPost]
    [Route("api/UploadFile")]
    public IHttpActionResult UploadFile()
    {
        try
        {
              var httpRequest = HttpContext.Current.Request;
                if (httpRequest.Files.Count > 0)
                {
                    foreach (string file in httpRequest.Files)
                    {
                        var postedFile = httpRequest.Files[file];
                        string filePath = ConfigurationManager.AppSettings["DocumentPath"];
                        postedFile.SaveAs(filePath);
                    }
                }
                return true;
        }
        catch (System.Exception ex)
        {
              throw ex;
        }
    }

    It hits the C# WebAPI UploadFile method code while debugging but the httpRequest.Files.Count is always 0 and httpRequest.Files does not contain any file due to which it does not execute the upload file code and file is not uploaded on server.

    Please advise how can I fix or resolve this issue ASAP.

    Regards,
    Pratham Jain



    Tuesday, September 22, 2020 6:41 PM

All replies

  • User303363814 posted

    Using the developer tools in your browser, what does the body of the Post contain?

    What is this.fileList[0] - according to the debugger!  (not what you think it is, what it actually is)

    Wednesday, September 23, 2020 7:29 AM
  • User-137472927 posted

    Hi Paul,

    The body of the Post contains two parameters: apiURL and formData object

    According to the debugger in browser, this.fileList[0] contains the file we attached which is being posted to the server side Web API C# code.    

    Regards,

    Pratham Jain

    Wednesday, September 23, 2020 6:26 PM
  • User303363814 posted

    Your code looks OK to me (it is basically what I am using) ... but ... what you say that you see in the body of the Post is not what that code generates.

    So  ... are you not showing the actual code or are you not telling us the correct body of the request?

    The code about credentials and httpHeaders makes me think that what you have shown is not your actual client code.  Your statement that apiUrl is in the body is also odd, since your code does not put it in the body (again, is this really the code? Or, have you misread the body of the Post)

    I would advise you to make a 'test' method on the client side which does the simplest possible Post.  Check that it produces a Post body which consists of only FormData.  Then, on the server side, check that the request.Content is of type multipart/form-data

    Wednesday, September 23, 2020 11:26 PM
  • User-474980206 posted

    which version of angular are you using? It needs to be 8+ to support FormData.

    Saturday, September 26, 2020 3:52 PM