Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8922445
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:50:10+00:00 2026-06-15T06:50:10+00:00

I am trying to create an upload page where I upload a .swf file

  • 0

I am trying to create an upload page where I upload a .swf file and then I add the filename to my database. Previously I have been able to achieve this, however, now it seems to give me the error “Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index” My code is as follows:

@{
Page.Title = "Add Game";

//Variables
var GameName = "";
var Tags = "";
var Gamefile = "";

//Required fields
Validation.RequireField("Name", "Please give the game a name.");
Validation.RequireField("file", "Please upload a file.");
//Maximum name length
Validation.Add("Name",
    Validator.StringLength(
        maxLength: 100,
        errorMessage: "Name must be less than 100 characters")
        );
//SWF file validation
Validation.Add("file",
    Validator.Regex(@"^.*\.(swf|SWF)$", "Invalid filetype, you must upload a .swf flash file")
    );

    if (IsPost && Validation.IsValid()) {
    var db = Database.Open("Surgestuff");
    var gCat = "";
    var fileData = Request.Files[0];
    var fileName = Guid.NewGuid().ToString() + ".swf";
    var fileSavePath = Server.MapPath("~/upload/" + fileName);
    var AddBy = WebSecurity.CurrentUserName;
    gCat=Request["formCat"];
    Gamefile = fileName;
    fileData.SaveAs(fileSavePath);
    var SQLINSERT = "INSERT INTO Games (Name, file_path, Category, AddBy) " + "VALUES (@0, @1, @2, @3)";
    db.Execute(SQLINSERT, GameName, Gamefile, gCat, AddBy);
    Response.Redirect("~/Games");

    }
}

And for some reason, even when i have a file submitted, the

var fileData = Request.Files[0]; gives me that error

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:50:12+00:00Added an answer on June 15, 2026 at 6:50 am

    The Web Pages 2 Validation helpers do not work with an input type="file". They only work with elements that are included in the Request.Form collection. A file upload appears in the Request.Files collection.

    There are a couple of approaches you can take to validate the file upload. You can use ModelState:

    if(IsPost && Request.Files[0].ContentLength == 0){
        ModelState.AddError("file", "You must choose a file");
    }
    
    if (IsPost && Validation.IsValid() && ModelState.IsValid) {
        // etc 
    

    Or you can add a hidden field, and when the form is submitted, populate its value with that of the file upload via JavaScript. Then you can use the new Validation helpers as you are currently trying to do, but on the hidden field instead.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to have a user upload a file and then my function will
I am trying to create a PHP file to upload images to my website.
I am trying to upload a file with playframework. I create a form and
I'm trying to create an upload file, and email as an attachment form where
I'm trying to create a simple file upload form for my website. I'm using
I'm trying to create a controller to upload photos in my MVC4 application. But
I'm trying to create a dialog which will allow users to upload an image.
I've been trying to do simple file uploads but I am unable to find
I'm trying to create a page that has a header, subheader, leftnav, rightnav, main,
I'm trying to learn the blobstore API... and I'm able to successfully upload files

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.