1

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?

4

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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