FileOperationAboutSharePoint.cs没有测试过 #sharepoint
string listName = System.Web.HttpContext.Current.Request.Form["listName"];
int listItemId = Convert.ToInt32(System.Web.HttpContext.Current.Request.Form["listItemId"]);
string fileName = System.Web.HttpContext.Current.Request.Form["fileTitle"];
fileName = DateTime.Now.ToString("yyyy-dd-hh-mm-ss") + "-" + fileName;
HttpFileCollection fileCollection = System.Web.HttpContext.Current.Request.Files;
if (fileCollection.Count > 0)
{
SPList list = web.Lists[listName];
for (int i = 0; i < fileCollection.Count; i++)
{
HttpPostedFile file = fileCollection[i];
var filePath = ProcessUploadFile(file); //这个应该是本地路径
SPListItem item = list.GetItemById(listItemId);
byte[] bytes = System.IO.File.ReadAllBytes(filePath);
site.AllowUnsafeUpdates = true;
web.AllowUnsafeUpdates = true;
// Add the file to the ListItem as an Attachment
SPAttachmentCollection attachments = item.Attachments;
var fileType = filePath.Substring(filePath.LastIndexOf("."));
fileName += fileType;
attachments.Add(fileName, bytes);
item.Update();
site.AllowUnsafeUpdates = false;
web.AllowUnsafeUpdates = false;
}
jsonData = JsonConvert.SerializeObject(new { result = true });
}else
{
jsonData = JsonConvert.SerializeObject(new { result = false, error = "没有文件上传" });
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
using System.Net;
using FileOperationAboutSharePoint.SPCopyService;
namespace FileOperationAboutSharePoint.FileOperation
{
public class SPServiceOperation : IFileOperation
{
public bool UploadFileToSPSite(string domain, string userAccount, string pwd, string documentLibraryUrl, string localFilePath, ref string statusInfo)
{
bool isSuccess = false;
try
{
string fileName = Path.GetFileName(localFilePath);
string tempFilePath = string.Format("{0}{1}", Path.GetTempPath(), fileName);
File.Copy(localFilePath, tempFilePath, true);
FileStream fs = new FileStream(tempFilePath, FileMode.Open, FileAccess.Read);
BinaryReader br = new BinaryReader(fs);
byte[] fileContent = br.ReadBytes((int)fs.Length);
br.Close();
fs.Close();
Copy service = CreateCopy(domain, userAccount, pwd);
service.Timeout = System.Threading.Timeout.Infinite;
FieldInformation fieldInfo = new FieldInformation();
FieldInformation[] fieldInfoArr = { fieldInfo };
CopyResult[] resultArr;
service.CopyIntoItems(
tempFilePath,
new string[] { string.Format("{0}{1}", documentLibraryUrl, fileName) },
fieldInfoArr,
fileContent,
out resultArr);
isSuccess = resultArr[0].ErrorCode == CopyErrorCode.Success;
if (!isSuccess)
{
statusInfo = string.Format("Failed Info: {0}", resultArr[0].ErrorMessage);
}
}
catch (Exception ex)
{
statusInfo = string.Format("Failed Info: {0}", ex.Message);
isSuccess = false;
}
return isSuccess;
}
public bool DownloadFileFromSPSite(string domain, string userAccount, string pwd, string documentUrl, string localFilePath, ref string statusInfo)
{
bool isSuccess = false;
try
{
Copy service = CreateCopy(domain, userAccount, pwd);
service.Timeout = System.Threading.Timeout.Infinite;
FieldInformation[] fieldInfoArr;
byte[] fileContent;
service.GetItem(documentUrl,out fieldInfoArr,out fileContent);
if (fileContent != null)
{
FileStream fs = new FileStream(localFilePath, FileMode.Create, FileAccess.Write);
fs.Write(fileContent, 0, fileContent.Length);
fs.Close();
isSuccess = true;
}
else
{
statusInfo = string.Format("Failed Info: {0}不存在", documentUrl);
isSuccess = false;
}
}
catch (Exception ex)
{
statusInfo = string.Format("Failed Info: {0}", ex.Message);
isSuccess = false;
}
return isSuccess;
}
private Copy CreateCopy(string domain, string userAccount, string pwd)
{
Copy service = new Copy();
if (String.IsNullOrEmpty(userAccount))
{
service.UseDefaultCredentials = true;
}
else
{
service.Credentials = new NetworkCredential(userAccount, pwd, domain);
}
return service;
}
}
}