8

Below given is a merge function which is intended to merge all docx files in a folder and produce the merged file.

public  void Merge()
{  
    try
    {

        string sid = Request.QueryString["studid"];
        string stud = sid.ToString();

        string ds = HttpContext.Current.Server.MapPath(("~\\StudentBinder") + "\\Temp4\\");
        if (Directory.Exists(ds))
        {
            DirectoryInfo d = new DirectoryInfo(ds);
            FileInfo[] Files = d.GetFiles("*" + stud + "*.docx");

              // stud added for differentiating b/w users

            string[] filepaths = new string[Files.Length];
            int index = 0;

            foreach (FileInfo file in Files)
            {
                filepaths[index] = file.Name;
                index++;
            }


                using (WordprocessingDocument myDoc = WordprocessingDocument.Open(Server.MapPath(filepaths[0]), true))

                {
                    for (int i = 1; i < filepaths.Length; i++)
                    {
                        MainDocumentPart mainPart = myDoc.MainDocumentPart;
                        string altChunkId = "AltChunkId" + i.ToString();
                        AlternativeFormatImportPart chunk = mainPart.AddAlternativeFormatImportPart(AlternativeFormatImportPartType.WordprocessingML, altChunkId);
                        using (FileStream fileStream = File.Open(@filepaths[i], FileMode.Open))
                        {
                            chunk.FeedData(fileStream);
                        }
                        AltChunk altChunk = new AltChunk();
                        altChunk.Id = altChunkId;
                        mainPart.Document.Body.InsertAfter(altChunk, mainPart.Document.Body.Elements<DocumentFormat.OpenXml.Wordprocessing.Paragraph>().Last());
                        mainPart.Document.Save();
                        myDoc.Close();
                    }
                }
        }
    }
    catch (Exception ex)
    {
    }
}

but the line

using (WordprocessingDocument myDoc = WordprocessingDocument.Open(Server.MapPath(filepaths[0]), true))

raises the error The specified package is invalid. The main part is missing.

I dont know what goes wrong in that statement. any suggestions are welcomed. Thanks in advance.

Binto
  • 99
  • 1
  • 1
  • 11

2 Answers2

4

You probably do Server.MapPath twice: in the beginning

string ds = HttpContext.Current.Server.MapPath(("~\\StudentBinder")+"\\Temp4\\")

and in the line

using (WordprocessingDocument myDoc = WordprocessingDocument.Open(Server.MapPath(filepaths[0]), true))

You can rewrite this line as

using (WordprocessingDocument myDoc = WordprocessingDocument.Open(filepaths[0], true))

and you also need to populate filepaths array from file.FullName, not file.Name:

foreach (FileInfo file in Files)
{
    filepaths[index] = file.FullName;
    index++;
}
  • if we remove the server.mappath from that place , it raises an exception - Access to the path 'C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\11.0\IEP4TEMP-1310-2.docx' is denied. – Binto Jan 16 '17 at 09:21
  • try changing HttpContext.Current.Server.MapPath(("~\\StudentBinder") + "\\Temp4\\" to Server.MapPath("~\\StudentBinder\\Temp4\\") maybe? – Heorhiy Pavlovych Jan 16 '17 at 09:25
  • but to solve this issue earlier i had followed what was said in http://stackoverflow.com/questions/9108399/how-to-grant-full-permission-to-a-file-created-by-my-application-for-all-users – Binto Jan 16 '17 at 09:25
  • then, if your file C:\Program Files (x86)\Common Files\Microsoft Shared\DevServer\11.0\IEP4TEMP-1310-2.docx exists it is only permissions problem – Heorhiy Pavlovych Jan 16 '17 at 09:26
  • yeah.tried server.mappath but same result. @Heorhiy Pavlovych – Binto Jan 16 '17 at 09:32
  • Just make sure that you don't do Server.MapPath twice, then verify if file really exists and permissions granted – Heorhiy Pavlovych Jan 16 '17 at 09:34
  • I have a doubt. first of all how did that path came, I never gave a path to C directory.. – Binto Jan 16 '17 at 09:38
  • Try HostingEnvironment.MapPath in the beginning? – Heorhiy Pavlovych Jan 16 '17 at 09:41
  • also take a look at http://stackoverflow.com/questions/18105250/server-mappath-returning-a-path-with-a-folder-that-doesn-t-exists – Heorhiy Pavlovych Jan 16 '17 at 09:43
  • and in that path IEP4TEMP-1310-2.docx doesnt exist there.. wondering from where that path came – Binto Jan 16 '17 at 09:55
  • IEP4TEMP-1310-2.docx situates in a folder in D directory which I created. – Binto Jan 16 '17 at 09:58
  • I've just noticed another issue: foreach (FileInfo file in Files) { filepaths[index] = file.Name; index++; } – Heorhiy Pavlovych Jan 16 '17 at 10:05
  • Why file.Name? you should populate file.FullName, see my updated answer – Heorhiy Pavlovych Jan 16 '17 at 10:05
  • Is there any other possibility that this error(The specified package is invalid the main part is missing) occur while opening word document? generally what could it be? the issue which was solved yesterday still shows up sometime – Binto Jan 17 '17 at 07:49
  • It might be due to non-existing file passed to the Server.MapPath(...) as far as I can see – Heorhiy Pavlovych Jan 17 '17 at 08:04
1

Sometimes if we have just created a docx and the file size becomes zero. This was my case.

lizardkingLK
  • 25
  • 2
  • 8