0

My fileupload isn't working. I get this error each time I try: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index.

I'm doing all of this from the .cshtml file because, the previous web master deleted all of the source code when he was fired. So I only have the deployed files to work with and none of the controllers and models. I also cannot add any new functionality to the website.

I used this tutorial this tutorial following the single file upload.

Here is my code:

@using Microsoft.Web.Helpers;
@{
    Layout="~/Views/Shared/_FUllWidthLayout.cshtml";

    var message="";
    if(IsPost){
        if(Request.Files.Count > 0){ //<-- If more than 0 files, FAILS HERE
            try{
                var uploadedFile = Request.Files[0];  //<-- FAILS HERE
                var fileName = Path.GetFileName(uploadedFile.FileName);
                var fileSavePath = Server.MapPath("~/Documents/Jobs/" + fileName);
                uploadedFile.SaveAs(fileSavePath);
                message = Request.Files.Count + " files uploaded to " + fileSavePath;
            }
            catch(Exception e){
                message = e.Message;
            }
        }else{
            message = Request.Files.Count + " files uploaded.";
        }
    }
}

And my HTML:

<form id="uploadForm" method="post" enctype="multipart/form-data">
    <h3>FileUpload - Single-File Example</h3>
    <div class="inline-div">
        @FileUpload.GetHtml(
            initialNumberOfFiles:1,
            allowMoreFilesToBeAdded:false,
            includeFormTag:true,
            uploadText:"Upload")

        <small class="red">@message</small>
    </div>
</form>

I also tried taking the FileUpload control out of the form like this:

<h3>FileUpload - Single-File Example</h3>
<div class="inline-div">
    @FileUpload.GetHtml(
        initialNumberOfFiles:1,
        allowMoreFilesToBeAdded:false,
        includeFormTag:true,
        uploadText:"Upload")
    <small class="red">@message</small>
</div>

Message says "0 files uploaded." as seen here: enter image description here

Can anyone point out what I'm doing wrong and how I can fix it? Or is there another tool I can use to do the same work?

NMeneses
  • 152
  • 6
  • 20
  • You should check Request.Files before accessing index 0 with `Request.Files[0];` – devlin carnate Apr 08 '20 at 17:18
  • My apologies, I was doing that before, but I wanted to capture the actual error. – NMeneses Apr 08 '20 at 17:23
  • It might be worthwhile for you to inspect `Request.Files` to see it's structure / content in debugger. – devlin carnate Apr 08 '20 at 17:37
  • You have `includeFormTag: true`, but are wrapping the control in a `form` tag. If I'm not mistaken, you want `includeFormTag: false` if you're going to be providing the `form` tag yourself. – Heretic Monkey Jun 19 '20 at 18:28
  • I get the same result whether I place the fileupload control inside a form or not. – NMeneses Jun 19 '20 at 18:30
  • Have you tried including a form tag as in your first example, but set `includeFormTag` to false? See for example [How to use @FileUpload.GetHtml inside Html.BeginForm and sumbit FilesList](https://stackoverflow.com/q/12282988/215552) – Heretic Monkey Jun 23 '20 at 20:30
  • Yes, I've tried that as well. Actually, that was the very first way I tried it. – NMeneses Jun 23 '20 at 20:38

0 Answers0