My current project: is an asp.net mvc web application featuring a basic email form with file upload functionality and a send button.
The email sending function works correctly, however, the files attached to the emails are coming through as empty. Upon debugging the controller, an InputStream.ReadTimeout error is encountered in the line of code var myFiles = model.MyFiles;
.
Model (relevant sections)
public List<HttpPostedFileBase> MyFiles { get; set; }
public List<EmailAttachments> Attachments { get; set; }
HTML setup
<form id="data" method="post" enctype="multipart/form-data">
<input id="files" type="file" aria-label="files" multiple="multiple" name="MyFiles">
</form>
<input type="button" class="btn k-button k-primary" id="send" value="Send" />
Javascript implementation
$(function () {
$("#send").click(function () {
var form = $("#data");
var formData = new FormData(form[0]);
var files = form.find("#files")[0].files;
$.each(files, function() {
var file = $(this);
formData.append(file[0].name, file[0]);
});
$.ajax({
type: "POST",
url: url,
data: formData,
contentType: false,
processData: false,
success: function (response) {
alert("Success");
},
error: function (response) {
alert("Error");
}
});
});
Controller logic
[HttpPost]
public ActionResult SendCompanyEmail(GeneralEmailViewModel model)
{
...
var myFiles = model.MyFiles; // ReadTimeout issue arises here
if (myFiles != null)
{
foreach (var file in myFiles)
{
if (file != null && file.ContentLength > 0)
{
MemoryStream stream = new MemoryStream(file.ContentLength);
stream.Position = 0;
attachements.Add(new EmailAttachments
{
FileName = file.FileName,
ContentType = file.ContentType,
Stream = stream,
});
}
}
}
// Code for handling email sending goes here...
}
Any ideas on how to identify and resolve the cause of the InputStream.ReadTimeout error?