Take the 2-minute tour ×
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It's 100% free, no registration required.

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

1 Answer 1

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);
fStream.Close();
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]";
file.Item.SystemUpdate(false);

Note: I am using SystemUpdate to avoid new version.

share|improve this answer

Your Answer

 
discard

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.