Quantcast
Channel: ASP.NET Core
Viewing all articles
Browse latest Browse all 9386

Fetching form data and obtaining response

$
0
0

I am writing an app that allows one to upload several files and that has a table in the view dinamically updated according to the uploading.

Thus, I have a view with a form like this:

<form id="AddAttachmentFrm" enctype="multipart/form-data" onsubmit="afFileSubmitAjax(this); return false;" >
...<input type="submit" id="AddAttachment" name="AddAttachment" class="btn btn-primary" value="Upload" />
...</form>

The form submission triggers the following JavaScript method:

        async function afFileSubmitAjax(oFormElem) {
            const formData = new FormData(oFormElem);

            var response = await fetch("@Url.Action("PostFile","Home")", {
                method: 'POST',
                body: formData
            });

My controller contains the following function which deals with /Home/PostFile:

        [HttpPost]
        public JsonResult PostFile(IFormCollection form)
        {
            string id = "Error";

            try
            {
                IFormFile file = form.Files[0];
                string filename = Path.GetFileName(file.FileName);

                if (file.Length > 1048576)
                    return Json(String.Empty);
                FileCacheModel fcitem = AddTempFile(filename, file, comment);

                id = fcitem.id; // an unique id

                AddFileToDatabase(fcitem);
            }
            catch
            {
                return Json(String.Empty);
            }

            return Json(id);
        }

Now I would like to obtain the information that comes in the end of the function (return Json(id)). How can I accomplished that? 

I tried to see the variable response of the JavaScript snippet, and I can't find the information the method returns.

Could you help me?


Viewing all articles
Browse latest Browse all 9386

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>