如何在数组

时间:2015-08-27 12:28:09

标签: c# asp.net c#-4.0

我想在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;
                }
            }

1 个答案:

答案 0 :(得分:2)

您需要在path中收集List<string>的值,然后将结果传递给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中的文件名重复。