Kobi
Kobi

Reputation: 145

HttpPostedFileBase could not be found Visual studio 2019

enter image description here

hi, i'm working on a asp.net core mvc with visual studio 2019, (see attached for the mvc version (3.1.8). i want to use HttpPostedFileBase, but getting error could not be found, i've seen a solution suggesting to add using system.Web, and i did, but still getting this error, Any ideas?

Edit: Created the html :

 <div class="col-md-6">
            <div>
                <input id="CSVFile" type="file">
            </div>
            <button onclick="Submit()">submit</button>
        </div>

and javascript:

<script type="text/javascript">
        function Submit() {
            var pdata = new FormData();
            var files = $("#CSVFile").get(0).files;
            window.alert(files.Length);
            pdata.append('CSVFile', files[0]);
            $.ajax({
                url: "Index",
                type: "POST",
                data: pdata,
                processData: false,
                contentType: false,
                success: function (data) {
                    var input = $("#CSVFile");
                    input.replaceWith(input.val('').clone(true));
                }
            });

        }
    </script>

but the controller is not being called (it is defined HttpPost)

public ActionResult Index(IFormFile CSVFile)
{
return View();
}

Upvotes: 1

Views: 3971

Answers (1)

Yiyi You
Yiyi You

Reputation: 18199

.net core 3.1 doesn't contain HttpPostedFileBase.If you want to upload files,you can use IFormFile,here is an official tutorial.

Here is a simple demo to use IFormFile(from view to controller):

view:

<div class="row">
    <div class="col-md-6">
            <div>
                <input id="CertImageFile" type="file">
            </div>
            <button onclick="Submit()">submit</button>
    </div>
</div>
@section scripts{
    <script type="text/javascript">
        function Submit() {
            var pdata = new FormData();
            var files = $("#CertImageFile").get(0).files;
            pdata.append('CertImageFile', files[0]);
            $.ajax({
                url: "Submit",
                type: "POST",
                data: pdata,
                processData: false,
                contentType: false,
                success: function (data) {
                   var input = $("#CertImageFile");
                   input.replaceWith(input.val('').clone(true));
                }
            });
            
        }
    </script>
}

Controller:

[HttpGet]
    public IActionResult TestIFormFile()
    {
        return View();
    }
    [HttpPost]
    public IActionResult Submit(IFormFile CertImageFile)
    {
        return Ok();
    }

result: enter image description here

Upvotes: 1

Related Questions