Multiple file inputs
I am using Asp.net and requirement specifies i use html input to post files to server rather than asp:fileupload.[ Reason : Add more html input file controls similar to CPanel file manager.(i.e) clicking on link adds another file input from which user can select another file*.But when ever i post back Request Files collection is always empty.* HTML FORM:
<form method="post" enctype="multipart/form-data" action="documents.aspx">
<div>
<input type="file" name="attachment" id="attachment" onchange="validate(this)" />
<span class="none">Filename should be within (1-25) letters long. Can Contain only letters
& numbers</span>
<div id="moreUploads">
</div>
<div id="moreUploadsLink" style="display: none;">
<a href="javascript:addFileInput();">Attach another File</a></div>
<input type="submit" id="btnSubmit" />
</div>
</form>
Javascript:
var upload_开发者_JAVA技巧number = 2;function addFileInput() {
try {
var fileUpload = document.getElementById("attachment");
var elemSpan = nextElement(fileUpload).cloneNode(true);
var elemDiv = document.getElementById("moreUploads");
var d = document.createElement("div");
var file = document.createElement("input");
file.setAttribute("type", "file");
file.setAttribute("id", "attachment" + upload_number);
file.setAttribute("onchange", "validate(this)");
d.appendChild(file);
d.appendChild(elemSpan);
elemDiv.appendChild(d);
upload_number++;
} catch (err) { alert(err);}}
the validate(this) is a function that validates file types on client.When validation succeeds then the link to add more file inputs is enabled. Could someone throw somelight on this.
I think you need to set the name
attribute of the inputs. Add this:
file.setAttribute("name", "attachment" + upload_number);
Update
Another problem you appear to have is that you are trying to nest a form tag inside another one (since your masterpage will typically have a <form runat="server">
in it already). Your uploads will not work in this situation because the main form does not have the proper enctype value. Normally, if you use the asp.net FileUpload control, it will automatically set the proper enctype to multipart/form-data on the main form tag for you. However, since you are using just plain html inputs, asp.net does not know that you are trying to upload files and therefore does not set the enctype for you.
So what you need to do is:
a) get rid of the form tag you have above (don't need it)
b) in your codebehind for this page, put the following line in the handler for the page_init
event:
this.Form.Enctype = "multipart/form-data";
精彩评论