Ext.net 文件上传,删除,下载。

前台

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="XUI._Default" %>

<%@ Register Assembly="Ext.Net" Namespace="Ext.Net" TagPrefix="ext" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>

<script type="text/javascript">
var prepareCommand = function(grid, command, record, row) {
// you can prepare group command
if (command.command == 'Delete') {
alert("asdf");
// command.hidden = true;
// command.hideMode = 'visibility'; //you can try 'display' also

}
};

var prepareGroupCommand = function(grid, command, groupId, group) {
// you can prepare group command
};

function linkRenderer(value, meta, record) {
return String.format("<a href='\{1}\'target='_blank'>{0}</a>", value, record.data.ows_EncodedAbsUrl);
}
</script>

</head>
<body>
<ext:ResourceManager ID="ResourceManager1" runat="server" />
<form id="form1" runat="server">
<div>
</div>
<div>
<table>
<tr>
<td>
<div>
<ext:Store ID="Store1" runat="server" OnBeforeStoreChanged="Store1_BeforeStoreChanged"
OnRefreshData="Store1_RefreshData">
<Reader>
<ext:JsonReader IDProperty="ows_ID">
<Fields>
<ext:RecordField Name="ows_ID" />
<ext:RecordField Name="ows_LinkFilenameNoMenu" />
<ext:RecordField Name="ows_Modified" />
<ext:RecordField Name="ows_EncodedAbsUrl" />
<ext:RecordField Name="ows__UIVersionString" />
<ext:RecordField Name="ows_File Size" />
</Fields>
</ext:JsonReader>
</Reader>
<SortInfo Field="ows_Modified" Direction="DESC" />
</ext:Store>
<ext:GridPanel ID="GridPanel1" runat="server" StoreID="Store1" StripeRows="true"
Title="附件" Height="240">
<ColumnModel ID="ColumnModel1" runat="server">
<Columns>
<ext:Column ColumnID="ows_ID" Header="ID" Width="20" DataIndex="ows_ID" MenuDisabled="true" Hidden="true"/>
<ext:Column ColumnID="ows_LinkFilenameNoMenu" Header="文件名称" Width="300" MenuDisabled="true"
DataIndex="ows_LinkFilenameNoMenu" Sortable="false">
<Renderer Fn="linkRenderer" />
</ext:Column>
<ext:Column ColumnID="ows_EncodedAbsUrl" Header="下载地址" Hidden="true" MenuDisabled="true"
DataIndex="ows_EncodedAbsUrl" />
<ext:Column ColumnID="ows_Modified" Header="修改时间" Width="150" DataIndex="ows_Modified"
MenuDisabled="true" />
<ext:Column ColumnID="ows__UIVersionString" Header="版本" Width="50" DataIndex="ows__UIVersionString"
MenuDisabled="true" />
<ext:Column ColumnID="ows_File Size" Header="大小" Width="50" DataIndex="ows_File Size"
MenuDisabled="true" />
<ext:ImageCommandColumn>
<Commands>
<ext:ImageCommand Icon="Delete" CommandName="Delete" Text="删除">
</ext:ImageCommand>
</Commands>
</ext:ImageCommandColumn>
</Columns>
</ColumnModel>
<DirectEvents>
<Command OnEvent="DeleteClick">
<EventMask ShowMask="true" Msg="正在删除..." />
<ExtraParams>
<ext:Parameter Name="ows_ID" Value="record.data.ows_ID" Mode="Raw"></ext:Parameter>
<ext:Parameter Name="ows_EncodedAbsUrl" Value="record.data.ows_EncodedAbsUrl" Mode="Raw"></ext:Parameter>
</ExtraParams>
<Confirmation BeforeConfirm="if (command=='Delete') return true;" ConfirmRequest="true" Message="确定删除?" Title="提示" />
</Command>
</DirectEvents>
<TopBar>
<ext:Toolbar ID="Toolbar1" runat="server">
<Items>
<ext:Label ID="Label1" runat="server" Text="附件上传:">
</ext:Label>
<ext:FileUploadField ID="FileUploadField1" runat="server" Width="200" Icon="Attach"
EmptyText="" ButtonText="浏览">
</ext:FileUploadField>
<ext:Button ID="btnSave" runat="server" Text="上传" Icon="Add">
<DirectEvents>
<Click OnEvent="UploadClick">
<EventMask ShowMask="true" Msg="正在上传..." />
</Click>
</DirectEvents>
</ext:Button>
</Items>
</ext:Toolbar>
</TopBar>
<BottomBar>
<ext:PagingToolbar ID="PagingToolbar1" runat="server" PageSize="5" StoreID="Store1" />
</BottomBar>
<LoadMask ShowMask="true" />
</ext:GridPanel>
</div>
</td>
</tr>
</table>
</div>
<div>
<asp:PlaceHolder ID="PlaceHolder1" runat="server" />
</div>
</form>
</body>
</html>

 

后台

using System;
using Ext.Net;
using System.Collections.Generic;
using System.Data;
using System.Xml;
using XDataAccess;
using System.IO;
using System.Web.UI.WebControls;
namespace XUI
{
public partial class _Default : System.Web.UI.Page
{
public DataProvider tmpDataProvider = new DataProvider();
public string RowID { get; set; }
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
var result = tmpDataProvider.GetDataTableByListName("http://XXXX:47834/CCC", "201203", string.Empty);
if (result != null)
{
Store1.DataSource = result;
Store1.DataBind();
}
else
{
Store1.DataSource = new List<Attachment>();
Store1.DataBind();
}
}
}

protected void UploadClick(object sender, DirectEventArgs e)
{
int tmpCount = FileUploadField1.FileName.LastIndexOf('\\');
string tmpFileName = FileUploadField1.FileName;
tmpFileName = tmpFileName.Remove(0, tmpCount + 1);
string desString = "http://XXXX:47834/CCC/201203/" + tmpFileName;

 

if (this.FileUploadField1.HasFile)
{
MossWeb.UploadFile(FileUploadField1.FileName, desString, FileUploadField1.FileBytes);
X.Msg.Show(new MessageBoxConfig
{
Buttons = MessageBox.Button.OK,
Icon = MessageBox.Icon.INFO,
Title = "提示",
Message = "文件上传成功!"
});
}
else
{
X.Msg.Show(new MessageBoxConfig
{
Buttons = MessageBox.Button.OK,
Icon = MessageBox.Icon.ERROR,
Title = "提示",
Message = "请浏览文件!"
});
}
var result = tmpDataProvider.GetDataTableByListName("http://XXXX:47834/CCC", "201203", string.Empty);
if (result != null)
{
Store1.DataSource = result;
Store1.DataBind();
}
else
{
Store1.DataSource = new List<Attachment>();
Store1.DataBind();
}
}


protected void Command(object sender, DirectEventArgs e)
{
X.Msg.Alert("Command", e.ExtraParams["ows_ID"] + " : " + e.ExtraParams["ows_EncodedAbsUrl"]).Show();
}

protected void DeleteClick(object sender, DirectEventArgs e)
{


MossWeb.DeleteFile("http://XXXX:47834/CCC", "201203", e.ExtraParams["ows_ID"], e.ExtraParams["ows_EncodedAbsUrl"].Replace("%20", " "));

var result = tmpDataProvider.GetDataTableByListName("http://XXXX:47834/CCC", "201203", string.Empty);
if (result != null)
{
Store1.DataSource = result;
Store1.DataBind();
}
else
{
Store1.DataSource = new List<Attachment>();
Store1.DataBind();
}
}

 

protected void Store1_RefreshData(object sender, StoreRefreshDataEventArgs e)
{
var result = tmpDataProvider.GetDataTableByListName("http://XXXX:47834/CCC", "201203", string.Empty);
if (result != null)
{
Store1.DataSource = result;
Store1.DataBind();
}
else
{
Store1.DataSource = new List<Attachment>();
Store1.DataBind();
}
}

protected void Store1_BeforeStoreChanged(object sender, BeforeStoreChangedEventArgs e)
{
var result = tmpDataProvider.GetDataTableByListName("http://XXXX:47834/CCC", "201203", string.Empty);
if (result != null)
{
Store1.DataSource = result;
Store1.DataBind();
}
else
{
Store1.DataSource = new List<Attachment>();
Store1.DataBind();
}
}
}
}

MOSSWeb

 

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using XDataAccess.ListsService;
using XDataAccess.SiteDataService;
using XDataAccess.CopyService;

namespace XDataAccess
{
public class MossWeb
{

 

/// <summary>
/// 获取列表库下的子项目
/// </summary>
/// <param name="theWebPath"></param>
/// <param name="theListName"></param>
/// <returns></returns>
public static System.Xml.XmlNode GetListItemsByListName(string theWebPath, string theListName, string strFolder)
{
ListsService.Lists listsWS = new ListsService.Lists();
if (string.IsNullOrEmpty(strFolder))
{
listsWS.Url = theWebPath + "/_vti_bin/lists.asmx";
}
else
{
listsWS.Url = theWebPath + "/" + theListName + "/_vti_bin/lists.asmx";
}
listsWS.UseDefaultCredentials = true;
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.LoadXml("<Document><Query /><ViewFields /><QueryOptions><Folder>" + theListName + "/" + strFolder + "</Folder></QueryOptions></Document>");
System.Xml.XmlNode listQuery = doc.SelectSingleNode("//Query");
System.Xml.XmlNode listViewFields = doc.SelectSingleNode("//ViewFields");
System.Xml.XmlNode listQueryOptions = doc.SelectSingleNode("//QueryOptions");
Guid g = GetWebID(theWebPath);
System.Xml.XmlNode items = listsWS.GetListItems(theListName, string.Empty, listQuery, listViewFields, string.Empty, listQueryOptions, g.ToString());
return items;
}

/// <summary>
/// 生产GUID
/// </summary>
/// <param name="theWebPath"></param>
/// <returns></returns>
public static Guid GetWebID(string theWebPath)
{
SiteDataService.SiteData siteDataWS = new SiteDataService.SiteData();
siteDataWS.UseDefaultCredentials = true;
SiteDataService._sWebMetadata webMetaData;
SiteDataService._sWebWithTime[] arrWebWithTime;
SiteDataService._sListWithTime[] arrListWithTime;
SiteDataService._sFPUrl[] arrUrls;
string roles; string[] roleUsers; string[] roleGroups;
siteDataWS.Url = theWebPath + "/_vti_bin/sitedata.asmx";
uint i = siteDataWS.GetWeb(out webMetaData, out arrWebWithTime, out arrListWithTime, out arrUrls, out roles, out roleUsers, out roleGroups);
Guid g = new Guid(webMetaData.WebID);
return g;
}

/// <summary>
/// 上传文件
/// </summary>
/// <param name="theCopySource"></param>
/// <param name="theCopyDest"></param>
/// <param name="fileData"></param>
public static string UploadFile(string theCopySource, string theCopyDest, byte[] fileData)
{
CopyService.Copy myCopyService = new CopyService.Copy();
myCopyService.Credentials =
System.Net.CredentialCache.DefaultCredentials;

string copySource = theCopySource;
string[] copyDest = { theCopyDest };


CopyService.FieldInformation myFieldInfo = new
CopyService.FieldInformation();
CopyService.FieldInformation[] myFieldInfoArray = { myFieldInfo };
byte[] myByteArray = fileData;
CopyService.CopyResult myCopyResult = new CopyService.CopyResult();

CopyService.CopyResult[] myCopyResultArray = { myCopyResult };

try
{
uint myCopyUint = myCopyService.CopyIntoItems(copySource, copyDest,
myFieldInfoArray, myByteArray, out myCopyResultArray);

return myCopyUint.ToString();

}
catch (Exception ex)
{
return string.Empty;
}
}

/// <summary>
/// 删除文件
/// </summary>
/// <param name="theWebPath"></param>
/// <param name="theListName"></param>
/// <param name="theID"></param>
/// <param name="theFileRef"></param>
public static void DeleteFile(string theWebPath, string theListName, string theID, string theFileRef)
{
/*Declare and initialize a variable for the Lists Web service.*/
ListsService.Lists listService = new ListsService.Lists();

/*Authenticate the current user by passing their default
credentials to the Web service from the system credential cache.*/
listService.Credentials =
System.Net.CredentialCache.DefaultCredentials;

/*Set the Url property of the service for the path to a subsite.*/
listService.Url = theWebPath + "/_vti_bin/lists.asmx";

/*Get Name attribute values (GUIDs) for list and view. */
System.Xml.XmlNode ndListView = listService.GetListAndView(theListName, "");
string strListID = ndListView.ChildNodes[0].Attributes["Name"].Value;
string strViewID = ndListView.ChildNodes[1].Attributes["Name"].Value;

/*Create an XmlDocument object and construct a Batch element and its
attributes. Note that an empty ViewName parameter causes the method to use the default view. */
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
System.Xml.XmlElement batchElement = doc.CreateElement("Batch");
batchElement.SetAttribute("OnError", "Continue");
batchElement.SetAttribute("ListVersion", "1");
batchElement.SetAttribute("ViewName", strViewID);

/*Specify methods for the batch post using CAML. To update or delete,
specify the ID of the item, and to update or add, specify
the value to place in the specified column.*/
batchElement.InnerXml = string.Format("<Method ID='1' Cmd='Delete'><Field Name='ID'>{0}</Field><Field Name='FileRef'>{1}</Field></Method>", theID, theFileRef);

/*Update list items. This example uses the list GUID, which is recommended,
but the list display name will also work.*/
try
{
var tmpXmlNode = listService.UpdateListItems(strListID, batchElement);
}
catch (Microsoft.SharePoint.SoapServer.SoapServerException ex)
{

}
}

/// <summary>
/// 创建文件夹
/// </summary>
/// <param name="theWebPath"></param>
/// <param name="theListName"></param>
/// <param name="strFolder"></param>
/// <returns></returns>
public static string CreateForlder(string theWebPath, string theListName, string strFolder)
{
string strFolderName = "";
try
{
DwsService.Dws dwsService = new DwsService.Dws();
dwsService.Url = theWebPath + "/_vti_bin/Dws.asmx";
dwsService.Credentials = System.Net.CredentialCache.DefaultCredentials;
strFolderName = dwsService.CreateFolder(theListName.Replace(" ", "%20") + "/" + strFolder.Replace(" ", "%20"));
if (IsDwsErrorResult(strFolderName))
{
return "0";
}
else
{
return "1";
}
}
catch (Exception ex)
{
return "0";
}
}

/// <summary>
/// 创捷文件夹结果
/// </summary>
/// <param name="ResultFragment"></param>
/// <returns></returns>
private static bool IsDwsErrorResult(string ResultFragment)
{
System.IO.StringReader srResult = new System.IO.StringReader(ResultFragment);
System.Xml.XmlTextReader xtr = new System.Xml.XmlTextReader(srResult);
xtr.Read();
if (xtr.Name == "Error")
{
return true;
}
else
{
return false;
}
}


}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值