I have code like this to accept file upload from Web UI:
byte[] addBytes = new byte[] { 45, 45 };
var uploadedFiles = this.HttpContext.Request.Form.Files;
foreach (var uploadedFile in uploadedFiles)
{
filename = uploadedFile.FileName;
fileFullPath = Path.Combine(uploadFolderName, filename);
using (Stream bodyStream = uploadedFile.OpenReadStream())
{
byte[] contents = await bodyStream.ToByteArrayAsync((int)this.HttpContext.Request.ContentLength);
if (filename.ToLower().EndsWith(".docx"))
{
contents.Append<byte>(addBytes[0]);
contents.Append<byte>(addBytes[1]);
}
System.IO.File.WriteAllBytes(fileFullPath, contents);
}
}
the addBytes was supposed to fix the problem but it did not make any difference. When I'm trying to open the docx document, I'm still prompted with this message: "Word found unreadable content in ..... Do you want to recover the contents of this document? If you trust the source of this document, click Yes" .
Does anyone know how to make WriteAllByte work without tripping that message? This code works with other formats, and even some .docx would open fine too. Only most .docx are corrupted