-1

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?

Nave Tseva
  • 868
  • 8
  • 24
  • 47
  • What do you mean saying it does not work? – Viktor S. Mar 29 '13 at 10:52
  • Define "don't work." What is the actual runtime behavior? Are there any error messages? What is in `Request.Files`? When you inspect the form post with browser tools (Firebug, etc.) what actually gets posted to the server? – David Mar 29 '13 at 10:52
  • I got this error message: System.ArgumentOutOfRangeException was System.Collections.Specialized.NameObjectCollectionBase.BaseGet(Int32 index) – Nave Tseva Mar 29 '13 at 10:55
  • On which line are you getting that error? – Viktor S. Mar 29 '13 at 10:55
  • 1
    is this posted complete code of file upload? – Naresh Pansuriya Mar 29 '13 at 10:56
  • I got the error in this line : int Size = Request.Files[0].ContentLength / 1024; Yes, this is the complete code – Nave Tseva Mar 29 '13 at 10:56
  • Are you sure error rise when you upload more than one file? it looks more like it happens when NO files uploaded. – Viktor S. Mar 29 '13 at 10:58
  • Now I see that it doesn't work at all, but in other page with one file upload it works... I don't know what is going on here – Nave Tseva Mar 29 '13 at 11:02
  • Does that error rise after you submit a form or even when you simply open that page? – Viktor S. Mar 29 '13 at 11:03
  • Can you show full HTML of that form? Possibly, you have some not closed tag or something else and your HTML is broken and inputs appear to be outside the form and not submitted. Also, ensure that you have set `method="post" enctype="multipart/form-data"` in your form – Viktor S. Mar 29 '13 at 11:04

2 Answers2

2

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;
    //.....
}
Viktor S.
  • 12,736
  • 1
  • 27
  • 52
  • Thank you. Now there is now error, but how can I upload more than one file? maybe with for loop? – Nave Tseva Mar 29 '13 at 11:15
  • 1
    See updated answer. Something like that should work. Just put everything inside foreach loop and replace all `Request.Files[0]` with `f` (or how you will call that variable) – Viktor S. Mar 29 '13 at 11:20
  • Thank you very much, but now I got this error : Cannot implicitly convert type 'System.Web.HttpPostedFile' to 'System.Web.HttpPostedFileBase' on this line :foreach (HttpPostedFile f in Request.Files) Why? – Nave Tseva Mar 29 '13 at 11:49
  • 1
    Hm. Strange. But check out updated answer. That is another approach. Also, you can simply try to change `foreach (HttpPostedFile f in Request.Files) {` to `foreach (HttpPostedFileBase f in Request.Files) {` – Viktor S. Mar 29 '13 at 11:52
  • But wait, do you really have HttpPostedFile in foreach statement? Maybe you have there HttpPostedFileBase? Request.Files contains HttpPostedFiles object and that error should not appear with my code. – Viktor S. Mar 29 '13 at 11:54
  • I don't think so, I'm a studend and learning this subject so I don't know very well everything, with the FOR loop it is better, the *only* problem is that the smae file upload, and the other files aren't... why? (sorry for all of this questions..) – Nave Tseva Mar 29 '13 at 11:57
  • Only small files are uploaded because of this conditionL `if (Size <= 512) {` Which accepts only files smaller than 512Kb – Viktor S. Mar 29 '13 at 12:00
0

I suggest you to use this uploadify library, it's free

Because basic Upload file asp.net don't offer possibility of multiple downloading

link : http://www.uploadify.com/download/

Aghilas Yakoub
  • 28,516
  • 5
  • 46
  • 51