Solution 1 :

You can use the following code for this problem.

<form action="#" asp-controller="" asp-action="">
  <a
    href="javascript:void(0);"
    class="btn btn-icon fs-xl mr-1"
    data-toggle="tooltip"
    data-original-title="Attach files"
    data-placement="top"
  >
    <div>
      <input type="file" id="choose-file" name="formFile" />
      <label for="choose-file">
        <i class="fal fa-paperclip color-fusion-300"></i
      ></label>
    </div>
  </a>

  <a
    href="javascript:void(0);"
    class="btn btn-icon fs-xl mr-1"
    data-toggle="tooltip"
    data-original-title="Insert photo"
    data-placement="top"
  >
    <i class="fal fa-camera color-fusion-300"></i>
  </a>

  <button
    class="btn btn-info shadow-0 ml-auto"
    type="button"
    id="submit"
    onclick="SubmitForm()"
  >
    Post
  </button>

  <script>
    // Submit action
    function SubmitForm(e) {
      const allData = new FormData(e);
      fetch("[any_url]", {
        body: allData,
        method: "POST",
        mode: "no-cors",
        headers: { "Content-Type": "multipart/form-data" },
      })
        .then((res) => res.json())
        .then((data) => console.log("Form submitted"))
        .catch((rej) => console.log("any rejection happened"));
    }
  </script>
</form>


Or I think, you should MVC technology.

Solution 2 :

Is it possible when the image is selected it upload in wwwroot?

Yes, it can be achieved on file change event. Please have a look on following code snippet.

Controller To Load View:

public IActionResult UploadButNoRefresh()
        {
            return View();
        }

View:

    <form enctype="multipart/form-data" asp-controller="" asp-action="">
        <a href="javascript:void(0);" class="btn btn-icon fs-xl mr-1" data-toggle="tooltip" data-original-title="Attach files" data-placement="top">

            <div>
                <input type="file" required onChange="UploadImage()" id="selectedFile" name="formFile" />
                <label for="choose-file"> <i class="fal fa-paperclip color-fusion-300"></i></label>
            </div>


        </a>
        <button class="btn btn-info shadow-0 ml-auto " type="submit" id="submit" onclick="addCode()">Post</button>
        <div class="col-md-3">
            <strong><span class="label label-primary" id="statusSpan" style="margin-left:1px"></span></strong>
        </div>
    </form>
    @section scripts {
    <script src="https://ajax.aspnetcdn.com/ajax/jQuery/jquery-3.2.1.min.js"></script>
    <script>
        $('#statusSpan').hide();
        var UploadImage = function () {
            var data = new FormData;
            var file = $("#selectedFile").get(0);
            var files = file.files;

            if (files == null) {
                alert("Select File");
                return false;
            }


            for (var i = 0; i < files.length; i++) {
                data.append('filearray', files[i]);
                data.append('filesize', files[i].size);
            }
            data.append('path', "img\selectedFile\");
            console.log(file);

            $.ajax({
                url: '@Url.Action("AutoFileUpload", "LoopUpload")',
                type: "POST",
                data: data,
                contentType: false,
                processData: false
            }).done(function (result) {
                console.log(result);
                if (result.status == "success") {
                    $('#statusSpan').show();
                    $('#statusSpan').text("Uploaded!");
                }

            }).fail(function (result) {
                if (result.status != "success") {
                    $('#status').text("Faild!");

                }
            });
        }
    </script>
}

Controller To Handle Auto Upload:

public async Task<IActionResult> AutoFileUpload(IEnumerable<IFormFile> filearray, String path)
        {

         

            if (filearray.FirstOrDefault().FileName == null)
            {
                return Content("File not selected");
            }


            var filePath = Path.Combine(_environment.WebRootPath, "ListUpload", filearray.FirstOrDefault()?.FileName);
            using (FileStream stream = new FileStream(filePath, FileMode.Create))
            {
                await filearray.FirstOrDefault().CopyToAsync(stream);
                stream.Close();
            }

            return Json(new { status = "success", imagename = filearray.FirstOrDefault()?.FileName });

        }

Note: ListUpload is the folder under wwwroot you could refer the screenshot below:

enter image description here

Output:

enter image description here

Problem :

user select image and submit on wwwroot folder.
i just want to submit the selected file but it refresh it all on submit button the submit button is post post the selected data on page how can i submit the image on select and then on submit button it show on page dont refresh the page.

 <form enctype="multipart/form-data" asp-controller="" asp-action="">
                                <a href="javascript:void(0);" class="btn btn-icon fs-xl mr-1" data-toggle="tooltip" data-original-title="Attach files" data-placement="top">

                                    <div>
                                        <input type="file" id="choose-file" name="formFile" />
                                        <label for="choose-file"> <i class="fal fa-paperclip color-fusion-300"></i></label>
                                    </div>


                                </a>

                                <a href="javascript:void(0);" class="btn btn-icon fs-xl mr-1" data-toggle="tooltip" data-original-title="Insert photo" data-placement="top">
                                    <i class="fal fa-camera color-fusion-300"></i>
                                </a>


                                <button class="btn btn-info shadow-0 ml-auto " id="submit" onclick="addCode()">Post</button>
                            </form>

i had tried this it submit the image where i want to but it also refresh which i dont want to do.

Comments

Comment posted by Jaromanda X

form submission does that

Comment posted by developer.mozilla.org/en-US/docs/Web/API/Fetch_API/Using_Fetch

form

Comment posted by Emil Viesná

AJAX is the best option.

Comment posted by Tim

Call preventDefault on your form action

Comment posted by ethry

You can use

Comment posted by jiya

this will work but the issue is i cant use submit button to submit it that post button is to post the selected data on page not for submitting. is it possible when the image is selected it upload in wwwroot

Comment posted by Abdumannon Shamsiyev

Of course. Can you show me your code that problem is in?

By