protected void OkButton_Click(object sender, EventArgs e)
{
if (fileUpload.HasFile)
{
string strLoginName = HttpContext.Current.User.Identity.Name.ToString(); //获取用户名
string folderTemp = strLoginName.Substring(strLoginName.LastIndexOf('\') + 1);
SPSecurity.RunWithElevatedPrivileges(delegate()
{
try
{
string FileFolder = Server.MapPath("~/_layouts/images/" + folderTemp + "Upfile/");
if (!Directory.Exists(FileFolder)) //根目录
{
Directory.CreateDirectory(FileFolder);//判断上传目录是否存在 自动创建
}
string strFilePath = FileFolder + fileUpload.FileName;
fileUpload.SaveAs(Server.MapPath("~/_layouts/images/" + folderTemp + "Upfile/" + fileUpload.FileName));
AddAttachment(strFilePath);
}
catch (Exception ex)
{
throw ex;
}
finally
{
string strFileFolder = Server.MapPath("~/_layouts/images/" + folderTemp + "Upfile/");
if (Directory.Exists(strFileFolder)) //根目录
{
//Directory.CreateDirectory(strFileFolder);//判断上传目录是否存在 自动创建
Directory.Delete(strFileFolder, true);
}
}
});
}
else
{
labMess.Visible = true;
}
}
public void AddAttachment(string originalImagePath)
{
int taskID = int.Parse(hfTaskID.Value);
string strUrl = hfProjectUrl.Value;
try
{
using (SPWeb spWeb = new SPSite(strUrl).OpenWeb())
{
SPList spList = spWeb.Lists["任务"];
SPQuery oQuery = new SPQuery();
oQuery.Query = "<Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + taskID + "</Value></Eq></Where>";
SPListItemCollection listItemCollection = spList.GetItems(oQuery);
if (listItemCollection.Count > 0)
{
SPListItem item = listItemCollection[0];
item["Status"] = "已提交";
string fileName = fileUpload.FileName;
SPAttachmentCollection attach = item.Attachments;
if (attach != null)
{
for (int i = 0; i < attach.Count; i++)
{
if (attach[i] == fileName)
{
attach.Delete(attach[i]);
}
}
}
hfProjectTitle.Value = fileName;
//HttpPostedFile UpFile = fileUpload.PostedFile;
item.Attachments.Add(fileName, GetAttachmentData(originalImagePath));
spWeb.AllowUnsafeUpdates = true;
item.Update();
}
}
SendInForm();
}
catch (Exception e)
{
throw e;
}
}
public byte[] GetAttachmentData(string originalImagePath)
{
try
{
using (FileStream fStream = System.IO.File.OpenRead(originalImagePath))
{
//Stream StreamObject = UpFile.InputStream;//建立数据流对像
byte[] btData = new byte[fStream.Length];
fStream.Read(btData, 0, btData.Length);
fStream.Close();
return btData;
}
}
catch (Exception e)
{
return null;
}
}
{
if (fileUpload.HasFile)
{
string strLoginName = HttpContext.Current.User.Identity.Name.ToString(); //获取用户名
string folderTemp = strLoginName.Substring(strLoginName.LastIndexOf('\') + 1);
SPSecurity.RunWithElevatedPrivileges(delegate()
{
try
{
string FileFolder = Server.MapPath("~/_layouts/images/" + folderTemp + "Upfile/");
if (!Directory.Exists(FileFolder)) //根目录
{
Directory.CreateDirectory(FileFolder);//判断上传目录是否存在 自动创建
}
string strFilePath = FileFolder + fileUpload.FileName;
fileUpload.SaveAs(Server.MapPath("~/_layouts/images/" + folderTemp + "Upfile/" + fileUpload.FileName));
AddAttachment(strFilePath);
}
catch (Exception ex)
{
throw ex;
}
finally
{
string strFileFolder = Server.MapPath("~/_layouts/images/" + folderTemp + "Upfile/");
if (Directory.Exists(strFileFolder)) //根目录
{
//Directory.CreateDirectory(strFileFolder);//判断上传目录是否存在 自动创建
Directory.Delete(strFileFolder, true);
}
}
});
}
else
{
labMess.Visible = true;
}
}
public void AddAttachment(string originalImagePath)
{
int taskID = int.Parse(hfTaskID.Value);
string strUrl = hfProjectUrl.Value;
try
{
using (SPWeb spWeb = new SPSite(strUrl).OpenWeb())
{
SPList spList = spWeb.Lists["任务"];
SPQuery oQuery = new SPQuery();
oQuery.Query = "<Where><Eq><FieldRef Name='ID' /><Value Type='Counter'>" + taskID + "</Value></Eq></Where>";
SPListItemCollection listItemCollection = spList.GetItems(oQuery);
if (listItemCollection.Count > 0)
{
SPListItem item = listItemCollection[0];
item["Status"] = "已提交";
string fileName = fileUpload.FileName;
SPAttachmentCollection attach = item.Attachments;
if (attach != null)
{
for (int i = 0; i < attach.Count; i++)
{
if (attach[i] == fileName)
{
attach.Delete(attach[i]);
}
}
}
hfProjectTitle.Value = fileName;
//HttpPostedFile UpFile = fileUpload.PostedFile;
item.Attachments.Add(fileName, GetAttachmentData(originalImagePath));
spWeb.AllowUnsafeUpdates = true;
item.Update();
}
}
SendInForm();
}
catch (Exception e)
{
throw e;
}
}
public byte[] GetAttachmentData(string originalImagePath)
{
try
{
using (FileStream fStream = System.IO.File.OpenRead(originalImagePath))
{
//Stream StreamObject = UpFile.InputStream;//建立数据流对像
byte[] btData = new byte[fStream.Length];
fStream.Read(btData, 0, btData.Length);
fStream.Close();
return btData;
}
}
catch (Exception e)
{
return null;
}
}