I want to send txt file to action and get text words count without submiting form.
my codes in action works well (this action get HttpPostedFileBase and using with stream reader return count)so problem is in View.
this is My Code in View to send file to view and get result:
<script>
$("#MyForm").submit(function (event) {
event.preventDefault();
var files = $('#fileInput').files[0]);
$.ajax({
type: "Post",
url: "/Counter/GetCount",
enctype: "multipart/form-data",
data: files
}).done(function (result) {
$("#result").html(result);
});
</script>
if you can, fix my code or give me new code i want to see your suggestions for doing this.
Edit code from comment:
[HttpPost]
public ActionResult GetCount(HttpPostedFileBase file)
{
using (StreamReader reader = new StreamReader(file.FileName))
{
string result = reader.ReadToEnd();
return PartialView(result.Count());
}
}
2
Answers
You can try to replace ajax request code with:
You can try to use FormData to pass files to action with ajax.Here is a working demo:
view:
Action:
Result: