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?
SharePoint Stack Exchange is a question and answer site for SharePoint enthusiasts. It only takes a minute to sign up.Sign up to join this community
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.