I made a dynamically multi upload form with file type inputs. When you select a file new file input will appear. When submit the form all inputs captured by “Request.Files” at code behind.
There is no problem when i generate those file in a .aspx file but when i use Web User Control (.ascx) page “Request.Files” add 1 more request (for the blank file input).
Why ascx count the balnk input?
I share the codes below:
MultiImage.ascx
<ul id="imagesToUpload">
<li><input type="file" class="imageToUpload" runat="server" /></li>
</ul>
<script type="text/javascript">
$('.imageToUpload').live('change', function () {
$(this).parent().append('<div class="imageInfoContainer"><span>Yüklenen görsel: ' + $(this).val() + ' </span><div class="deleteImage">Sil<div><div>');
if ($('input:file').length < <%= MaxImageNumber %>) {
$(this).parent().parent().append('<li><input type="file" class="imageToUpload" runat="server" /></li>');
}
});
$('.deleteImage').live('click', function () {
$(this).parent().parent().remove();
if ($('input:file').length < 1) {
$('#imagesToUpload').html('<li><input type="file" class="imageToUpload" runat="server" /></li>');
}
});
</script>
MultiImage.ascx.cs
var imageCollection = Request.Files; //Counts all file inputs included the blank one
You mean why does
HttpRequestobject count that input? Because it is present in the request. You can however easily check if it is empty or not by looking at theHttpPostedFile.ContentLengthproperty of each element inside Files collection.