0

I'm writing a Reactjs app and post a file and a string as follow to ASP.Net Core 2.0 back end api project. I wanted to post a file and a string value to the back end. But it always show error.

                f.append("File",filesArray[i][0]);
                f.append("member_code", this.state.member_code);
                axios.post(apiBaseUrl
                    , f, {
                    headers: {'Content-Type': 'multipart/form-data'}
                })
                .then((response) => {
                    var result = response.data;
                    if(result[0].status == "1")
                    {
                        this.state.filesNames.push(result[0].filename);
                        if((i +1) == filesArray.length){
                            window.HideModal();
                            this.setState({filesPreview: null, filesToBeSent: null});
                            this.props.onClick(this.state.filesNames);
                        }
                    }
                });

In my ASP.Net core project I tried as below:

[HttpPost]
        [Route("upload")]
        public async Task<IActionResult> Upload(FileUploadViewModel[] model)
        {
            var file = model.File;
            var member_code = "test";
            if (file.Length > 0)
            {
                string path = Path.Combine(_env.WebRootPath, "uploadFiles/member_files/" + member_code);
                bool exists = System.IO.Directory.Exists(path);

                if (!exists)
                    System.IO.Directory.CreateDirectory(path);

                using (var fs = new FileStream(Path.Combine(path, file.FileName), FileMode.Create))
                {
                    await file.CopyToAsync(fs);
                }
            }

            return clsCommonFunctions.ConstructFileUploadResponse(clsConfig.status_success, file.FileName);
        }

But in the ASP.Net core function, I cannot accept both File and string value that I passed as multipart/form-data.

Anyone advise me how can I accept file and a string value as FormData in ASP.Net Core project.

Thanks.

1 Answer 1

1

This article helped me a lot. I just solved my problem by using [FromBody] attribute to fetch specific value from Request Form-Data. And the final code is as below:

[HttpPost]
        [Route("upload")]
        public async Task<IActionResult> Upload([FromForm] FileUploadViewModel model, [FromForm] string member_code)
        {
            var file = model.File;

            if (file.Length > 0)
            {
                string path = Path.Combine(_env.WebRootPath, "uploadFiles/member_files/" + member_code);
                bool exists = System.IO.Directory.Exists(path);

                if (!exists)
                    System.IO.Directory.CreateDirectory(path);

                using (var fs = new FileStream(Path.Combine(path, file.FileName), FileMode.Create))
                {
                    await file.CopyToAsync(fs);
                }
            }

            return clsCommonFunctions.ConstructFileUploadResponse(clsConfig.status_success, file.FileName);
        }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.