locked
Unable to move .docx files through my web service

    Question

  • I have been asked to support an older Web application which was developed with Visual Studio 2003 & WSE 2.0.  The application has apparently worked fine for several years.  However, when the users upgraded Office, the Web application corrupted their Office 2007 documents.  I have boiled the problem down to the following code in the web service, which writes the file to the disk.

     

    ' Write that buffer into a file
    OutFileStream = System.IO.File.OpenWrite(ServerFilePath)
    OutFileStream.Write(buffer, 0, buffer.Length)
    OutFileStream.Close()

    I also built a test application in VS 2003 which simply copies a file byte by byte.  This also corrupts a .docx file but not a .doc file.  Why?




    Tuesday, July 07, 2009 1:38 PM

Answers

  • I couldn't reproduce the problem, I wonder if something else is going on -- perhaps an antivirus program is interfering with the File I/O?  Here's the program I used to do a simple byte-level copy of a docx file:

        class Program
        {
            static void Main(string[] args)
            {
                if (args.Length != 2)
                {
                    Console.WriteLine("Usage: exe src dest");
                    return;
                }

                byte[] buf;
                using (var src = File.OpenRead(args[0]))
                {
                    buf = new byte[src.Length];
                    src.Read(buf, 0, buf.Length);
                }

                using (var dest = File.Create(args[1]))
                {
                    dest.Write(buf, 0, buf.Length);
                }
            }
        }
    Monday, July 27, 2009 9:56 PM

All replies

  • Does it corrupt their Office XP documents?
    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of old ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects
    Tuesday, July 07, 2009 2:07 PM
  • I don't have access to a machine with Office XP on it.  What did Microsoft change in Office 2007 that would make this no longer work?
    Wednesday, July 08, 2009 12:01 PM
  • The file format. a .docx file is not the same format as .doc.

    BTW, while you're at it, you should make sure your Management knows that WSE 2.0 is very old and obsolete. Assume you get it fixed this time. In another couple of years, there may be nobody left who has even heard of WSE 2.0. It's really time to move to WCF before it's too late.


    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of old ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects
    Wednesday, July 08, 2009 12:52 PM
  • I am trying to come up with an estimate on what it would take to update the entire application.  I am also trying to patch the existing application so that it can work with Office 2007 files.

    I am just confused as to why the following code which performs a byte by byte copy of the file can create a usable copy of a .doc file and can not produce a usable copy of a .docx file.

    ' Read the file into a buffer
    Dim InFileStream As New FileStream(FromFilename, FileMode.Open, FileAccess.Read)
    Dim Buffer(CType(InFileStream.Length, Integer)) As Byte
    InFileStream.Read(Buffer, 0, InFileStream.Length)
    InFileStream.Close()

     

    ' Write the buffer to a file
    Dim OutFileStream As New FileStream(ToFilename, FileMode.OpenOrCreate, FileAccess.Write)
    OutFileStream.Write(Buffer, 0, Buffer.Length)
    OutFileStream.Close()

    A byte by byte comparison of the two files, original and copy, seems to indicate that they are identical yet; Office 2007 says that the copy is corrupt. Is the difference in the directory entry for the file?

    Wednesday, July 08, 2009 1:31 PM
  • Yes, creating a docx file this way will give error. May be you can use the following method to create docx files that are not corrupted.
    As per your current code, create the doc file. Then convert that into docx file as below.

    //Refer latest Microsoft word object library from 'COM' tab through 'Addreference'
    Microsoft.Office.Interop.Word.Application wordApp = new Microsoft.Office.Interop.Word.Application();
    wordApp.Documents.Open(ref Source, ref Unknown, //Source=filepath for source file (doc)
                     ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown,
                     ref Unknown, ref Unknown, ref Unknown, ref Unknown,ref Unknown);

                // Specifying the format in which you want the output file
                object format = Microsoft.Office.Interop.Word.WdSaveFormat.wdFormatDocumentDefault;

                //Changing the format of the document
                wordApp.ActiveDocument.SaveAs(ref Target, ref format, //Target=filepath of target file (docx)
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown, ref Unknown,
                        ref Unknown, ref Unknown);

                // for closing the application
                wordApp.Quit(ref Unknown, ref Unknown, ref Unknown);

    Wednesday, July 08, 2009 2:04 PM
  • Using the Office COM Automation from inside a service is not supported, doesn't work well, and is only licensed if all the users are licensed for the corresponding Office product.

    John Saunders
    WCF is Web Services. They are not two separate things.
    Use WCF for All New Web Service Development, instead of legacy ASMX or obsolete WSE
    Use File->New Project to create Web Service Projects
    Wednesday, July 08, 2009 2:14 PM
  • I couldn't reproduce the problem, I wonder if something else is going on -- perhaps an antivirus program is interfering with the File I/O?  Here's the program I used to do a simple byte-level copy of a docx file:

        class Program
        {
            static void Main(string[] args)
            {
                if (args.Length != 2)
                {
                    Console.WriteLine("Usage: exe src dest");
                    return;
                }

                byte[] buf;
                using (var src = File.OpenRead(args[0]))
                {
                    buf = new byte[src.Length];
                    src.Read(buf, 0, buf.Length);
                }

                using (var dest = File.Create(args[1]))
                {
                    dest.Write(buf, 0, buf.Length);
                }
            }
        }
    Monday, July 27, 2009 9:56 PM