简体   繁体   中英

Select uploaded files by key

In an ASP.NET application I want to process uploaded files. The HttpContext.Current.Request.Files.AllKeys contains the following:

[0]File2    
[1]File2    
[2]flTeklif    
[3]flTeklif    
[4]flTeklif    
[5]flTeklif

How can I select only the uploaded files that have key flTeklif into a List<HttpPostedFile> ?

I tried this:

var uploads = HttpContext.Current.Request.Files.AllKeys
                         .Where(s=>s.stringname == "flTeklif") 

But that only selects the keys, not the files. How can I select the Files.Where(key == "flTeklif") ?

HttpRequest.Files is an HttpFileCollection , whose AllKeys property is a string array.

So you can just use AllKeys.Where(s => s == "flTeklif") .

So far for the literal interpretation of your question, which is probably why you're pretty heavily down- and closevoted as it doesn't really make any sense.

If your actual question is "How can I select the files that have flTeklif as their key" , use:

var files = HttpContext.Current.Request.Files;
var result = new List<HttpPostedFile>();

for (int i = 0; i < files.AllKeys.Count; i++)
{
    if (files.AllKeys[i] == "flTeklif")
    {
        result.Add(files.AllKeys[i]);
    }
}

Then result will contain the files you're interested in.

OK i understand. may be

HttpContext.Current.Request.Files.Cast<HttpPostedFile>().Where(c => c.FileName.Contains("flTeklif")).ToList();

Use HttpFileCollection.GetKey(Int32) method. There is no mention that AllKeys array order is the similiar to the files HttpFileCollection .

 int i;
 HttpFileCollection MyFileColl = Request.Files;

 for( i= 0; i< MyFileColl.Count; i++)
 {
    if( MyFileColl.GetKey(i) == "flTeklif")
    {
       //...
    }
 }

You can use the following code to get a list of all uploaded files against a particular key

HttpFileCollection files = null;
try { files = HttpContext.Current.Request.Files; } catch { }
var allUploadedFilesAgainstThisKey = files.GetMultiple(singleKey);      // singleKey is a string
        // allUploadedFilesAgainstThisKey is of type IList<HttpPostedFile>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM