We're sorry Aspose doesn't work properply without JavaScript enabled.

Free Support Forum - aspose.com

Save document in DOCX by using FileStream corrupts the file

Hi Aspose team,

I have very simple test where I am opening DOCX document by using FileStream and saving it.
When I try to open this document afterwards Word 2013 is saying that there is a problem and it can try to fix it.

Why is this?

In debug folder you have also DOC document where save works ok.

Sample project is attached.
Source code is:

public void OpenSave()
{
    String path = "book1.docx";
    FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
    Aspose.Words.Document doc = new Aspose.Words.Document(fs);
    Aspose.Words.SaveFormat savFor = SaveFormat.Docx;

    String extension;
    extension = System.IO.Path.GetExtension(path);

    switch (extension.ToLower())
    {
        case ".doc":
            savFor = Aspose.Words.SaveFormat.Doc;
            break;
        case ".docm":
            savFor = Aspose.Words.SaveFormat.Docm;
            break;
        case ".docx":
            savFor = Aspose.Words.SaveFormat.Docx;
            break;
        case ".dot":
            savFor = Aspose.Words.SaveFormat.Dot;
            break;
        case ".dotm":
            savFor = Aspose.Words.SaveFormat.Dotm;
            break;
        case ".dotx":
            savFor = Aspose.Words.SaveFormat.Dotx;
            break;
    }
    doc.Save(fs, savFor);
    fs.Dispose();
}

Hi Oliver,

Thanks for your inquiry. Please use following highlighted code snippet to fix this issue.

String path = "book1.docx";
FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
Aspose.Words.Document doc = new Aspose.Words.Document(fs);
Aspose.Words.SaveFormat savFor = SaveFormat.Docx;
String extension;
extension = System.IO.Path.GetExtension(path);
switch (extension.ToLower())
{
    case ".doc":
        savFor = Aspose.Words.SaveFormat.Doc;
        break;
    case ".docm":
        savFor = Aspose.Words.SaveFormat.Docm;
        break;
    case ".docx":
        savFor = Aspose.Words.SaveFormat.Docx;
        break;
    case ".dot":
        savFor = Aspose.Words.SaveFormat.Dot;
        break;
    case ".dotm":
        savFor = Aspose.Words.SaveFormat.Dotm;
        break;
    case ".dotx":
        savFor = Aspose.Words.SaveFormat.Dotx;
        break;
}
fs.Dispose();
fs = new FileStream(MyDir + "Out.docx", FileMode.OpenOrCreate);
doc.Save(fs, savFor);
fs.Dispose();

Please let us know if you have any more queries.

Hi Tahir,

thanks, this helped.
I have to admit that I am confused - when I try to do Open/Save of .DOC file (my original coding) this works without problems. Saved document can be opened. With .DOCX this does not work.

Second is this not a performance issue:

  • open FileStream
  • close FileStream
  • open again same FileStream (I need to save in same document)
  • close FileStream

Why is there difference between .DOC and DOCX saving when using FileStream?

Thanks,
Oliver

Hi Oliver,

Thanks for your inquiry. As you are saving the output document to disk, I suggest you please use following code example. There is no need to create FileStream to save the document to disk.

Hope this helps you. Please let us know if you have any more queries.

String path = "book1.docx";
FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
Aspose.Words.Document doc = new Aspose.Words.Document(fs);
doc.Save(MyDir + "Out.docx", SaveFormat.Docx);
doc.Save(MyDir + "Out.doc", SaveFormat.Doc);

Hi Tahir,

unfortunately I need to use FileStream.
Aspose does not support path which is longer than 260 characters (Windows limitation) thus I am using AlphaFS component (which returns FileStream) and does not have limitation of 260 characters.

Of course, if you extend Aspose components I’ll switch back to “easier” approach to open and save documents :-).

BR,
Oliver

Hi Tahir,

yeap, that is exactly why I am using FS and it works with AlphaFS for long paths.

BR,
Oliver