如何获取数组中的多个文件路径

how to get multiple files path in an array

我想在 asp.net 中的一个文件上传控件中上传多个 pdf 文件,然后合并它, 当我传递静态路径和文件名时,这已经完成 如何动态地做到这一点 我的代码在这里

  if (FileUpload1.HasFile)
            {
                try
                {
                     HttpFileCollection uploadedVideoFiles = Request.Files;

                    // Get the HttpFileCollection

                    for (int i = 0; i < uploadedVideoFiles.Count; i++)
                    {
                        HttpPostedFile hpfiles = uploadedVideoFiles[i];
                      string fname = Path.GetFileName(hpfiles.FileName);

                        if (hpfiles.ContentLength > 0)
                        {
                            hpfiles.SaveAs(Server.MapPath("~/Images/") + Path.GetFileName(hpfiles.FileName));
                            hpfiles.SaveAs(Server.MapPath(Path.Combine(@"~/Images/", fname)));
                            string filepath = Server.MapPath(@"~/Images/");
                            string path = filepath + fname;
                        }
                    }
                    String[] files = @"C:\ENROLLDOCS\A1.pdf,C:\ENROLLDOCS\A@.pdf".Split(',');
                    MergeFiles(@"C:\ENROLLDOCS\New1.pdf", files);// merg is a method which merg 2 or more than 2 documents
                }
                catch (Exception ex)
                {
                    Label1.Text = "The file could not be uploaded. The following error occured: " + ex.Message;
                }
            }

您需要在 List<string> 中收集 path 的值,然后将结果传递给 MergeFiles()

我不太理解你的代码(你需要稍微清理一下),但你基本上需要的是:

var fileNames =
    uploadedVideoFiles.
        Select(uvf => {
            var fileName = Path.GetFileName(hpfiles.FileName);
            var destinatonPath = Path.Combine(Server.MapPath("~/images"), fileName);

            uvf.SaveAs(destinatonPath);

            return destinationPath;
        }).
        ToArray();

MergeFiles(@"C:\ENROLLDOCS\New1.pdf", fileNames);

注意 ~/images 中的重复文件名。