如何在ASP.NET中使用多个fileupload控件上传多个文件? [英] How to upload multiple files using multiple fileupload controls in ASP.NET?

查看:70
本文介绍了如何在ASP.NET中使用多个fileupload控件上传多个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个包含MULTIPLE FileUpload控件的网页(.aspx)。



I have a webpage(.aspx) which contains MULTIPLE FileUpload controls.

<asp:FileUpload ID="flpProspectus"  AllowMultiple="true" class="form-control" runat="server"  Style="width: 40%; margin-top: -12px;" />
<asp:FileUpload ID="flpOrientation"  AllowMultiple="true" class="form-control" runat="server"  Style="width: 40%; margin-top: -12px;" />





Default.aspx.cs



Default.aspx.cs

public void FileUpload1()
    {
        if (flpProspectus.HasFile)
        {
            string fileExtension = System.IO.Path.GetExtension(flpProspectus.FileName);
            int fileSize = flpProspectus.PostedFile.ContentLength;
            HttpFileCollection hfc = Request.Files;
            string[] arr = new string[5];
            for (int i = 0; i < hfc.Count; i++)
            {
                HttpPostedFile hpf = hfc[i];
                if (hpf.ContentLength > 0)
                {
                    hpf.SaveAs(Server.MapPath("~/college/fileupload3/") + System.IO.Path.GetFileName(hpf.FileName));
                    string filepath = Server.MapPath("~/college/fileupload3/");
                    string path = "college/fileupload3/" + hpf.FileName;
                    if (i < 5)
                    {
                        arr[i] = path;
                        path1 = arr[0]; path2 = arr[1]; path3 = arr[2]; path4 = arr[3]; path5 = arr[4];

                    }

                }
            }
        }
    }





我尝试了什么:



这里,Request.Files将集中来自FileUploadControls的所有文件。



我无法识别哪些文件来自特定的FileUpload控件?



我知道它可能在4.5但我目前的框架是4.0,我不想升级到4.5。任何使用现有4.0框架的解决方案??



帮助赞赏!



请注意:这不是DUPLICATE问题是我的要求是在单页上传和识别不同FileUpload控件的文件。



What I have tried:

Here, Request.Files will get collectively all files from both the FileUploadControls.

I am NOT able to IDENTIFY which file(s) are from specific FileUpload control?

I know its possible in 4.5 but my current framework is 4.0 and i don't want to upgrade to 4.5. Any solution using existing 4.0 framework??

Help appreciated!

Please note: This is not DUPLICATE question as my requirement is to upload and identify the files of different FileUpload controls on single page.

推荐答案

为什么不使用PostedFiles属性?您可能需要重写if(i< 5)...,不清楚您要在那里做什么。



更新目标框架4.0的解决方案

Why not use the PostedFiles property? You might need to rewrite the "if (i < 5)...", not clear what you trying to do there.

Updated solution to target Framework 4.0
if (flpProspectus.HasFile || flpOrientation.HasFile)
{
    string fileExtension = string.Empty; // System.IO.Path.GetExtension(flpProspectus.FileName);
    int fileSize = 0;// flpProspectus.PostedFile.ContentLength;
    HttpFileCollection hfc = Request.Files;
    string[] arr = new string[5];
    string path1, path2, path3, path4, path5 = string.Empty;
    string filePath = string.Empty;

    for (int i = 0; i < hfc.Count; i++)
    {
        if (hfc.GetKey(i) == "flpOrientation")
        {
            filePath = "college/fileupload3";
        }

        if (hfc.GetKey(i) == "flpProspectus")
        {
            filePath = "college/fileupload4";
        }

        fileExtension = System.IO.Path.GetExtension(hfc[i].FileName);
        fileSize = hfc[i].ContentLength;
        if (hfc[i].ContentLength > 0)
        {
            hfc[i].SaveAs(Server.MapPath(string.Format("~/{0}/", filePath)) + System.IO.Path.GetFileName(hfc[i].FileName));
            string filepath = Server.MapPath(string.Format("~/{0}/", filePath));
            string path = string.Format("{0}/", filePath) + hfc[i].FileName;
            if (i < 5)
            {
                arr[i] = path;
                path1 = arr[0]; path2 = arr[1]; path3 = arr[2]; path4 = arr[3]; path5 = arr[4];
            }
        }
    }
}


这篇关于如何在ASP.NET中使用多个fileupload控件上传多个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆