Problem Description: Upload files to SharePoint document library, I think there is 2 difficulties, which is better to solve file transfer, such as using FTP protocols. Berior possible It is the issue of permission. (The original solution is correct, just simulates the user, the domain name is written, haha,)
I want many people to meet this problem. My solution can solve some problems at a certain extent. (^_^), please refer to the description in MSDN for identity simulation. This segment does not contain code for directory verification.
This problem has been troubled. I have tried a variety of ways. But the same procedure, running in the Windows application, there is a problem in WebService. (But tested many times this evening, prove My method is effective, excited ....)
The following is the file upload program code implemented by the way of file monitoring: (the way to simulate the Windows user is used to simulate the current operational user)
private void fileSystemWatcher1_Created (object sender, System.IO.FileSystemEventArgs e) {Application.DoEvents (); WindowsImpersonationContext wic = null; if (! wic = null) wic.Undo (); try {System.IO.File.Copy (e .Fullpath, "c: // arestemp //" E.NAME, TRUE);
FileStream FStream; fstream = file.openread ("c: // arestemp //" E.NAME); byte [] contents = new byte [fstream.length]; FSTREAM.READ (Contents, 0, (int) fstream. Length); fstream.close (); if (file.exists ("c: // arestemp //" E.NAME) == true) {file.delete ("c: // arestemp ///" e. name);} wic = SPUtil.GetWindowsIdentity (); SPWeb myweb = new SPSite (SharePointURL) .OpenWeb (); myweb.AllowUnsafeUpdates = true; myweb.Files.Add (SharePointURL "/" e.Name, contents, true) ; // if (File.exists (E.FullPath)) // file.delete (E.FullPath); if (wic! = Null) wic.undo (); ARESLOG.Recordlog ("File upload success", "" , "" "" "" ", E.NAME, E.FullPath;} catch (Exception EE) {IF (wic! = null) wic.undo (); ARESLOG.Recordlog (" Upload File Exception "," ",", EE .Message, ee.stacktrace;}}
The following is the way I wrote through the ftp, transfer the file to the local, then read the local disk file, upload the code to the Sharpoint document library: (I need to pay attention to the writing of the domain name, this code has been successful, but the specific environment Different, it may also cause a certain difference)
[WebMethod] public string UpLoadFilesToSPS (string FtpServer, string FtpUser, string FtpPwd, string FtpPath, string FilesName, string FilesInfo, string targetUrl) {// ftp settings FtpClient myFTP = new FtpClient (); myFTP.Server = FtpServer; myFTP.Username = FtpUser; myFTP.Password = ftpPwd; myFTP.RemotePath = ftpPath; // filePath, info string [] fName = Microsoft.VisualBasic.Strings.Split (FilesName, ";", - 1, Microsoft.VisualBasic.CompareMethod.Text) String [] FINFO = Microsoft.visualBasic.Strings.Split (FilesInfo, ";", - 1, Microsoft.VisualBasic.comPareMethod.Text); INT i = 0; // Check and create a local environment IF (Directory.exists "C: // arestemp") == false) {Directory.createdIRectory ("c: // arestemp");} foreach (string fdirectory in finfo) {if (Directory.exists ("c: // arestemp //" fdirectory) == false) {Directory.createdIRectory ("c: // arestemp //" fdirectory);}} // File download for (i = 0; i