![](/img/trans.png)
[英]Select multiple files at a time using FileUpload control in asp.net
[英]How to Save multiple files in asp.net 4.0 by Fileupload1 Control with multiple fileupload2 control?
如何通过Fileupload Control在多个文件上传控件中保存asp.net 4.0中的多个文件? 我有两个fileupload控件,一个用于图像,第二个用于thumbimage。 所以我想保存多个图像和拇指像?
当你在循环中时,你实际上用第二个文件覆盖了第一个文件。 我建议你创建一个文件列表,并添加到循环中的列表,如下所示。 然后,当您可以对第一个项目使用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);
}
}
更新:
现在要获取第一个文件,你在列表上调用FirstOrDefault(),如下所示:
fileList1.FirstOrDefault();
并获得第二个文件:
fileList1.Skip(1).FirstOrDefault();
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.