Uploading and processing multiple files using MVC

Indhi

I am trying to upload multiple files on my web application. Hence I am using IEnumerable<HttpPostedFileBase> class to loop inside each file. But I am getting an error stating -

Error System.Collections.Generic.IEnumerable<System.Web.HttpPostedFileBase> does not contain a definition for 'ContentLength' and no extension method 'ContentLength' accepting a first argument of type System.Collections.Generic.IEnumerable<System.Web.HttpPostedFileBase' could be found (are you missing a using directive or an assembly reference?)

This error is for all the properties present in the HttpPostedFileBase class. I am trying to edit that class but it doesnt allow. I tried creating a IEnumerable of HttpPostedFileBase class in my ViewModel, but it fails again. What am I missing here?

Update - Code: View

<div class="col-sm-8">                                  
 <input type="file" name="Files" id="file1" class="form-control" />
  <input type="file" name="Files" id="file2" class="form-control" />
  <input type="submit" value="Save" class="btn btn-default" name="Command"/>   
</div>

Controller

public ActionResult UploadFile(IEnumerable<HttpPostedFileBase> Files)
{
     foreach (var item in Files)
     { 
           if (Files != null && Files.ContentLength > 0)
           {
                FileUpload up = new FileUpload();
                up.PersonId = model.PersonId;
                up.FileName = System.IO.Path.GetFileName(Files.FileName);
                up.MimeType = Files.ContentType;
                up.FileContent = Files.Content;
                bll.AddFileUpload(up);
            }
     }
     return View();
}
Yuval Itzchakov

The problem is here:

foreach (var item in Files)
    if (Files != null && Files.ContentLength > 0)

You're using foreach to iterate the collection, but you still check the IEnumerable called File instead of each item. What you want is:

foreach (var item in Files)
    if (item != null && item.ContentLength > 0)

As a side note, you can filter out items using Enumerable.Where:

foreach (var item in Files.Where(file => file != null && file.ContentLength > 0))
{
    FileUpload up = new FileUpload
    {
         PersonId = model.PersonId,
         FileName = System.IO.Path.GetFileName(item.FileName),
         MimeType = item.ContentType,
         FileContent = item.Content,
     };
     bll.AddFileUpload(up);
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Uploading multiple files using Javascript

From Dev

Selecting multiple files and uploading them using Jersey

From Dev

Uploading multiple files via FTP using curl

From Dev

Uploading Multiple Files - MVC..is there a total file size limit?

From Dev

Uploading multiple files in a ModelForm

From Dev

AngularJs Uploading multiple files .then

From Dev

Multiple Files Uploading in Laravel

From Dev

Uploading multiple image files using okhttp v3.2.0

From Dev

Weird behaviour when uploading multiple files using AngularJS

From Dev

Uploading multiple single files using separate Dropzones in one form

From Dev

Uploading multiple files in PHP using HTML Form or cURL

From Dev

Uploading multiple files in PHP using HTML Form or cURL

From Dev

Uploading Multiple Files in Android using existing multipart post project

From Dev

Uploading multiple image files using okhttp v3.2.0

From Dev

Issues with Uploading Multiple files with PHP

From Dev

Uploading multiple files in the same request

From Dev

Multiple files uploading in multidimensional array

From Dev

PHP code for uploading multiple files

From Dev

Multiple files uploading not working in Codeigniter

From Dev

Uploading multiple files and renaming - PHP

From Dev

Multiple files not uploading in play framework

From Dev

Multiple files uploading not working in Codeigniter

From Dev

Uploading multiple files in Laravel 5

From Dev

Uploading files using the Kik Browser

From Dev

Uploading files using Django Admin

From Dev

Uploading and storing files using php

From Dev

Uploading files using selenium java

From Dev

Uploading and Downloading Files using fog

From Dev

Using ThreadPool for processing files

Related Related

HotTag

Archive