一、服务器端文件路径的获取
1、通过HttpContext.Current.Request.ApplicationPath应用程序所在路径,
HttpContext.Current.Request.ApplicationPath+“文件的详细地址”.
通过Server.mapath()获取文件的服务器路径.
2、服务器路径的详细代码(extjs)
window.js
function getFilePath(TempType)
{
var Pathtree = new Ext.tree.TreePanel( {
autoScroll:true,
animate:true,
width:205,
height:300,
enableDD:true,
containerScroll:true,
loader:new Ext.tree.TreeLoader( {
dataUrl:'Sys_SystemFileData.aspx'// OrgTreeJsonData.action就是要动态载入数据的请求地址,这里请求时会提交一个参数为node的值,值为root即new Tree.AsyncTreeNode()对象的id值
}),
listeners:{ "dblclick":function(node){
var obj;
if(TempType=='Upload')
{
obj=document.getElementById('ServerPath');
}
else
{
obj=document.getElementById('DownloadFilePath');
}
obj.value=node.attributes.customUrl;
PathWin.hide();
}
}
});
var Pathroot = new Ext.tree.AsyncTreeNode({
text:'WebApp',
draggable:false,
id:'01'//默认的node值:?node=-100
});
Pathtree.setRootNode(Pathroot);
// Pathtree.render();
PathWin = new Ext.Window({
title:"选择文件上传所属的路径",
width:250,
height:350,
plain:true,
iconCls:"addicon",
//不可以随意改变大小
resizable:false,
//是否可以拖动
//draggable:false,
defaultType:"textfield",
labelWidth:100,
collapsible:true, //允许缩放条
closeAction : 'hide',
closable:true,
plain : true,
//弹出模态窗体
modal: 'true',
buttonAlign:"center",
bodyStyle:"padding:10px 0 0 15px",
items:[Pathtree]
})
PathWin.show();
Pathroot.expand();
}
aspx文件
private DataTable treeData;
/// <summary>
/// 生成数据的数据表
/// </summary>
public DataTable TreeData
{
get { return treeData; }
set { treeData = value; }
}
public string ReturnStr = "";
public DataTable tempTable = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
InitData();
}
private void InitData()
{
string nodeId = Request.Params["node"] + "";
Utility Common = new Utility();
string NowPath = "";
if (string.IsNullOrEmpty(nodeId) || nodeId == "01")
{
NowPath = Common.GetRootPath();
NowPath = Server.MapPath(NowPath);
}
else
{
NowPath = nodeId;
}
CreatTable(); //创建临时表
SetTableValue(nodeId, NowPath);//向临时表中放入数据
tempTable.DefaultView.Sort = "id asc";
this.TreeData = tempTable;
ReturnStr = this.GetTreeJsonData();
}
public bool FormatValue(ref string forName, ref string forValue, DataRow forDr)
{
switch (forName.ToLower())
{
case "leaf":
forValue = (forValue == "1" ? "true" : "false");
break;
default:
forValue = "/'" + forValue + "/'";
break;
}
return true;
}
private void CreatTable()
{
tempTable = new DataTable();
DataColumn dataColumn1 = new DataColumn("id", typeof(string));
tempTable.Columns.Add(dataColumn1);
DataColumn dataColumn2 = new DataColumn("text", typeof(string));
tempTable.Columns.Add(dataColumn2);
DataColumn dataColumn3 = new DataColumn("leaf", typeof(string));
tempTable.Columns.Add(dataColumn3);
DataColumn dataColumn4 = new DataColumn("customUrl", typeof(string));
tempTable.Columns.Add(dataColumn4);
}
private void SetTableValue(string nodeId, string NowPath)
{
DirectoryInfo d = new DirectoryInfo(NowPath);
int i = 0;
string FileId = "";
string FileFullName = "";
string CustomUrl = "";
int Index = 0;
FileInfo[] fis = d.GetFiles();
foreach (FileInfo fi in fis)
{
DataRow row = tempTable.NewRow();
i++;
if (i.ToString().Length == 1)
{
FileId = nodeId + "0" + i.ToString();
}
else
{
FileId = nodeId + i.ToString();
}
FileFullName = fi.FullName;
row["id"] = FileFullName.Replace("//", "");
row["text"] = fi.Name;
row["leaf"] = "1";
Index = FileFullName.LastIndexOf("WebApp") + 7;
CustomUrl = FileFullName.Substring(Index, FileFullName.Length - Index);
row["customUrl"] = CustomUrl.Replace("//", "");
tempTable.Rows.Add(row);
row = null;
}
DirectoryInfo[] dis = d.GetDirectories();
foreach (DirectoryInfo di in dis)
{
DataRow row = tempTable.NewRow();
i++;
if (i.ToString().Length == 1)
{
FileId = nodeId + "0" + i.ToString();
}
else
{
FileId = nodeId + i.ToString();
}
FileFullName = di.FullName;
row["id"] = FileFullName.Replace("//", "");
row["text"] = di.Name;
row["leaf"] = "0";
Index = FileFullName.LastIndexOf("WebApp") + 7;
CustomUrl = FileFullName.Substring(Index, FileFullName.Length - Index);
row["customUrl"] = CustomUrl.Replace("//", "");
tempTable.Rows.Add(row);
row = null;
}
}
public string GetTreeJsonData()
{
if (TreeData == null) throw new ArgumentNullException("数据不能为空!");
StringBuilder jsonBuilder = new StringBuilder();
string temValue = string.Empty;
string temName = string.Empty;
bool isHaving = false;
jsonBuilder.Append("[");
for (int i = 0; i < TreeData.Rows.Count; i++)
{
jsonBuilder.Append("{");
isHaving = false;
for (int j = 0; j < TreeData.Columns.Count; j++)
{
temName = TreeData.Columns[j].ColumnName;
temValue = TreeData.Rows[i][j].ToString();
if (FormatValue(ref temName, ref temValue, TreeData.Rows[i]) == false) continue;
isHaving = true;
jsonBuilder.Append("/'");
jsonBuilder.Append(temName);
jsonBuilder.Append("/':");
jsonBuilder.Append(temValue);
jsonBuilder.Append(",");
}
if (isHaving == true)
{
jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
}
jsonBuilder.Append("},");
}
jsonBuilder.Remove(jsonBuilder.Length - 1, 1);
jsonBuilder.Append("]");
return jsonBuilder.ToString();
}
/// <summary>
/// 输出json
/// </summary>
/// <param name="Json"></param>
public void ResponseStr(string Json)
{
this.Response.Clear();
this.Response.Write(Json);
this.Response.End();
}