0

Currently, I have this:

public ActionResult Add(FormCollection form, HttpPostedFileBase fr,  HttpPostedFileBase en, HttpPostedFileBase es)
{
   Upload(fr, "fr");
   Upload(en, "en");
   Upload(es, "es");
   ...
}

This works for what we're doing currently, but just learned of a new requirement where the system needs the ability to add other languages. This is the only part where I have an issue.

I tried:

public ActionResult Add(FormCollection form, HttpPostedFileBase[] fr)
{
   foreach(var file in fr)
   {
     Upload(file, "I'mStuck");
   }
   ...
}

as a test, but it will only have 1 element and it is the one where id/name = fr. Makes sense, but not particularly helpful for what I need.

I could do:

for (string file in Request.Files)
{
   ...
}

which would handle the upload component fine, but the issue is that unless I can force them to standardize against a whatever_langabbreviation.extension file format, which I can't, I'm not going to be able to know what the language abbreviation is.

How can I obtain the id/name fields for the input type=file objects within the controller?

Robert
  • 1,745
  • 5
  • 34
  • 61

1 Answers1

0

I was actually incorrect. The string returned is actually the id or name (think name, but considering I typically pair id/name, it works).

For the controller that renders the view initially, I did:

List<Languages> langs = db.Languages.ToList();

viewmodel.Languages = langs;

return View(viewmodel);

In the view itself:

foreach(Language lang in Model.Languages)
{
  // Label
  <input type="file" id="@lang.Abbreviation" name="@lang.Abbreviation" />
}

And in the post event:

foreach(string file in Request.Files)
{
  HttpPostedFileBase fb = Request.Files[file];
  Upload(fb, file);
}

And it handles as it is supposed to (Upload being a function that just adds a new item to another table.

Robert
  • 1,745
  • 5
  • 34
  • 61