简体   繁体   English

如何通过Fileupload1 Control在多个fileupload2控件中保存asp.net 4.0中的多个文件?

[英]How to Save multiple files in asp.net 4.0 by Fileupload1 Control with multiple fileupload2 control?

How to Save multiple files in asp.net 4.0 by Fileupload Control with multiple fileupload control? 如何通过Fileupload Control在多个文件上传控件中保存asp.net 4.0中的多个文件? I have two fileupload control one for image and second for thumbimage. 我有两个fileupload控件,一个用于图像,第二个用于thumbimage。 So I want to save multiple image and thumbimage ? 所以我想保存多个图像和拇指像?

You are actually overwriting the fist file with the second one when you are in the loop. 当你在循环中时,你实际上用第二个文件覆盖了第一个文件。 I would suggest you to create a list for the files and add to the list in the loop like below. 我建议你创建一个文件列表,并添加到循环中的列表,如下所示。 You will then have a list of file when you can use firstOrDefault() for the first item as well as use Skip() and Take() to select any item you want. 然后,当您可以对第一个项目使用firstOrDefault()以及使用Skip()和Take()选择您想要的任何项目时,您将拥有一个文件列表。

HttpFileCollection uploadedFiles = Request.Files;
List<HttpPostedFile> fileList1 = new List<HttpPostedFile>();
List<HttpPostedFile> fileList2 = new List<HttpPostedFile>();



for (int i = 0; i < uploadedFiles.Count; i++)
{
    HttpPostedFile hpf = uploadedFiles[i];
    var hpfKey = uploadedFiles.Keys[i];
    if (hpfKey.IndexOf("FileUpload1") > 0)
    {
        fileList1.Add(hpf);
    }
    if (hpfKey.IndexOf("FileUpload2") > 0)
    {
        fileList2.Add(hpf);
    }
}

Update: 更新:

Now to get the first file you call FirstOrDefault() on the list like below: 现在要获取第一个文件,你在列表上调用FirstOrDefault(),如下所示:

 fileList1.FirstOrDefault();

And to get the second file : 并获得第二个文件:

fileList1.Skip(1).FirstOrDefault();

HttpFileCollection uploadedFiles = Request.Files; HttpFileCollection uploadedFiles = Request.Files;

    int i = uploadedFiles.Count;


    List<HttpPostedFile> fileList1 = new List<HttpPostedFile>();
    List<HttpPostedFile> fileList2 = new List<HttpPostedFile>();

    if (i > 0)
    {
        for (int j = 0; j < i/2; j++)
        {
            fileList1.Add(uploadedFiles[j]);
        }
    }

    if (i > 0)
    {
        for (int j = i / 2; j < i; j++)
        {
            fileList2.Add(uploadedFiles[j]);
        }
    }

    int filecount = fileList1.Count;
   if (filecount > 0)
    {
        for (int j = 0; j < filecount; j++)
        {
     string image =  fileList1[j].FileName;
    fileList1[j].SaveAs(imagepath);
    string image =  fileList2[j].FileName;
    fileList2[j].SaveAs(imagepath);
    }
   }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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