I am sending an uploaded file from the html view to the controller using ajax but the file is received as null in the controller.
I tried using FormData but nothing happens or maybe I am not using it right, when I sent the file using html.BeginForm() it was read correctly in the controller but I don't want to use forms because it opens another page after submitting
Below is my controller
public void Upload_SCN_CA_File(FormCollection formCollection)
{
if (Request != null)
{
HttpPostedFileBase file = Request.Files["UploadedFile"];
if ((file != null) && (file.ContentLength > 0) && !string.IsNullOrEmpty(file.FileName))
{
string fileName = file.FileName;
Debug.WriteLine(fileName);
string fileContentType = file.ContentType;
byte[] fileBytes = new byte[file.ContentLength];
var data = file.InputStream.Read(fileBytes, 0, Convert.ToInt32(file.ContentLength));
}
}
}
Below is the JQuery ajax call
$("#upload").on("click",function () {
$.ajax({
type: "POST",
url: "/Order/Upload_SCN_CA_File",
data: {
enctype: 'multipart/form-data'
},
success: function (response) {
if (response.result == false) {
swal("Error", response.message, "error");
}
else {
swal("Success", response.message, "success");
}
}
});
});
Below is my html view
<form>
<div class="row">
<div class="col">
<input name="UploadedFile" id="upfile" type="file" />
</div>
</div>
<div class="row">
<div class="col">
<div class="btn btn-primary rounded" id="upload" style="margin:8px">Upload</div><br>
</div>
</div>
</form>
I expect the file to be sent correctly to the controller so I can read it correctly but it is received as null