SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Does anyone know why my programmatically uploaded Docx files arrive corrupted? I've seen more messages of this problem all over the internet without a solution.

Would it be possible for someone to post some code that would work?

share|improve this question
up vote 4 down vote accepted

Not sure about other examples but this one works for me (using application page)

Application page markup contains:

<input type="file" id="InputFile" runat="server" class="ms-fileinput" />

Code behind (eg. on button click):

var fStream = InputFile.PostedFile.InputStream;
var fileName = InputFile.PostedFile.FileName;
var contents = new byte[fStream.Length];
fStream.Read(contents, 0, (int)fStream.Length);
var list = List.GetList([TARGET LIBRARY URL HERE]);
var destinationFolder = list.RootFolder;
var strDestUrl = destinationFolder.Url + "/" + fileName;
var file = destinationFolder.Files.Add(strDestUrl, contents, true);

However I had experienced some corrupt file uploads in past (office docs) but only when I tried to include metadata while adding file (and not always). To avoid this I am setting all additional field values after file is added.

file.Item["[FIELD ITERNAL NAME"] = "[DATA]";

Note: I am using SystemUpdate to avoid new version.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.