I have a view which allow the user to upload multiple at a time as below
<label class="control-label">Files:</label>
<div id="fileUploadContainer">
<div id="uploadContainer" style="height:30px">
<input type="file" id="file1" name="file1" onchange="ValidateSingleInput(this);" />
</div>
<div class="controls">
<div id="fileappend">
<div id="fileappendContainer">
<output id="filesSize"></output>
</div>
</div>
</div>
</div>
<a id="btnAdd" href="#">Add Another</a>
Code "Add Another" Click:
$(function () {
$('#btnAdd').click(function (e) {
e.preventDefault();
var htmlFormatDiv = $("<div id='uploadContainer' style='height:30px'></div>");
var htmlFormatFile = $("<input type='file' onchange='ValidateSingleInput(this);' />");
var totalFileCount = $("#fileUploadContainer").children("div").length;
var fuData = document.getElementById('<%= file1.ClientID %>');
htmlFormatFile.attr("id", "file1");
htmlFormatFile.attr("name", "file1");
htmlFormatDiv.attr("id", "uploadContainer");
htmlFormatDiv.append(htmlFormatFile);
$("#fileUploadContainer").append(htmlFormatDiv);
$("#filesSize").append(htmlFormatDiv);
});
});
Code to restrict the file upload extension type and showing the file size
var _validFileExtensions = [".jpg", ".jpeg", ".pdf"];
function ValidateSingleInput(oInput) {
if (oInput.type == "file") {
var sFileName = oInput.value;
if (sFileName.length > 0) {
var fsize = sFileName.size;
if (fsize > 4048576) {
alert(fsize + " bites\nToo big!");
}
var blnValid = false;
for (var j = 0; j < _validFileExtensions.length; j++) {
var sCurExtension = _validFileExtensions[j];
if (sFileName.substr(sFileName.length - sCurExtension.length, sCurExtension.length).toLowerCase() == sCurExtension.toLowerCase())
{
blnValid = true;
$('#filesSize').append('Size - ' + ((oInput.files[0].size) / 1024 / 1024).toFixed(2) + 'mb' + '<br />');
break;
}
}
if (!blnValid) {
alert("Sorry, " + sFileName + " is invalid, allowed extensions are: " + _validFileExtensions.join(", "));
oInput.value = "";
return false;
}
}
}
return true;
}
Now when i Upload the files I am getting below O/P

The problem occurs when i click any of the existing browse button again browse another document. The size of the old file will remain there and the size of the new file will attach to the last filename irrespective of the browse button clicked.
Is there any way to avoid the same?

Also is there any other method which is simpler than this.
Controller
public ActionResult file_upload(IEnumerable<HttpPostedFileBase> file1)
{
foreach (var file in file1)
{
if (file != null)
{
if (file.ContentLength > 0)
{
}
}
}
}