I have the following HTML source:
<form name="AddTrack" id="add_track_form" action="AddTrack.aspx" method="post" runat="server">
<input type="file" name="file1"/><br />
<input type="file" style="margin-right: 52px;" name="file2" /><br />
<input type="file" style="margin-right: 52px;" name="file3" /><br />
<input type="file" style="margin-right: 52px;" name="file4" /><br />
<button type="submit" class="blue-button">הוסף מסלול</button>
</form>
With this ASPX - C# code:
if (Request.ContentLength != 0)
{
int Size = Request.Files[0].ContentLength / 1024;
if (Size <= 512)
{
string LocalFile = Request.Files[0].FileName;
int LastIndex = LocalFile.LastIndexOf(@"\") + 1;
string File = LocalFile.Substring(LastIndex, LocalFile.Length - LastIndex);
string Path = Server.MapPath(" ../images/tracks") + "..\\" + File;
Request.Files[0].SaveAs(Path);
Response.Write(@"The file was saved: " + Path);
}
else
{
Response.Write("The file is too big !");
}
}
else
{
Response.Write("Unknown Error !");
}
If I upload one file it works great, but I upload there is more than one upload input it don't work.
My question is why and how can I fix it?
As far as I can see, you just need to add enctype="multipart/form-data"
to your form:
<form name="AddTrack" id="add_track_form" action="AddTrack.aspx" method="post" runat="server" enctype="multipart/form-data">
http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2
The content type "application/x-www-form-urlencoded" is inefficient for sending large quantities of binary data or text containing non-ASCII characters. The content type "multipart/form-data" should be used for submitting forms that contain files, non-ASCII data, and binary data.
You are not using asp:FileUpload
control which adds that enctype automatically, so you should do that manually.
for(int i = 0; i < Request.Files.Count; i++) {
int Size = Request.Files[i].ContentLength / 1024;
if (Size <= 512)
{
string LocalFile = Request.Files[i].FileName;
//.....
}