页面javascript方法和几个相关的asp.net页面 UpLoadAttachs.aspx执行上传操作 AttachContent.aspx内容操作 DelFile.aspx删除操作
GetAttachs.aspx 获取附件显示
<script language="JScript">
//上传文件选择,触发上传
function fileChanged()
{
//上传附件2007-07-21
var tem=Form1.action; Form1.action="UpLoadAttachs.aspx?targetTable=T_EMAIL_ATTACH_FILE&targetPrimaryKey=MAIL_ATTACH_FILE_ID&targetForeignKey=MAIL_ID&targetKeyValue=" + <%=EmailId%> + "&RND"+Math.random()+"="+Math.random();
Form1.target="ifrUpLoad";
Form1.submit();
Form1.action=tem;
Form1.target="_self";
}
function UpSuccess()
{
RefreshAttachList();
}
//初始化,移动位置到屏幕之外
Form1.f.style.posLeft =-120;
function OpenAttach(AttachfileName,AttachFileID)
{//打开附件
var width =window.screen.availWidth;
var height =window.screen.availHeight;
var features = "top=0,left=0,width=" + width + ",height=" + height + ",toolbar=no,menubar=no,location=no,status=no,resizable=yes";
window.open("AttachContent.aspx/" +AttachfileName +"?AttachFileID=" +AttachFileID+"&RND"+Math.random()+"="+Math.random(),"",features);
}
function DeleteAttach(fileIds, table, pk, fk, val) {
//确认是否要删除
var msgbox=new ClientMessageBox();
msgbox.setPrePath("../oa/web/Common");
var rt=msgbox.DoModal("邮件","确认是否要删除附件?","Question","YesNo");
if(rt!="IDYes")
{
return 1;
}
try
{
var xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
xmlhttp.open("GET","DelFile.aspx?fileIds=" + fileIds + "&table=" + table + "&pk=" + pk + "&fk=" + fk + "&val=" + val ,false);
xmlhttp.send();
xmlhttp.close();
}
catch(e)
{
}
RefreshAttachList();
}
function RefreshAttachList(){//refresh the attach file list when new file uploaded
var val = <%=EmailId%>;
if(val==0)
val = -<%=UserId%>;
var strID;
var exchange = new DataExchange();
exchange.Initialize();
exchange.SetServerPath("GetAttachs.aspx");
exchange.SetCommand("GetAttachs");
exchange.AddParameter("targetTable","T_EMAIL_ATTACH_FILE");
exchange.AddParameter("targetPrimaryKey","MAIL_ATTACH_FILE_ID");
exchange.AddParameter("targetForeignKey","MAIL_ID");
exchange.AddParameter("targetKeyValue",<%=EmailId%>);
exchange.Call();
exchange.GetResult();
var root = exchange.GetResultNode("Attaches");
var canUploadDraft = root.getAttribute("ParamValue"); //can delete draft and
for(var i=attachList.rows.length; i>0;)
attachList.deleteRow(--i);
var node;
var row,cell,img,span;
var fileName;
var AttachNum=0;
for(var i=0; i<root.childNodes.length; i++){
node = root.childNodes[i];
if((i%2)==0)
row = attachList.insertRow();
cell = row.insertCell();
cell.style.fontSize = "13px";
cell.style.width = "50%";
img = new Image();
img.src = "http://www.cnblogs.com/images/att.gif";
cell.appendChild(img);
fileName = node.getAttribute("FileName");
span = document.createElement("<SPAN onclick='OpenAttach(""" + fileName + """," + node.getAttribute("ParamValue") + ")'>");
span.style.cursor="hand";
span.innerText = fileName;
cell.appendChild(span);
img = document.createElement("<IMG title='删除' onclick=""DeleteAttach('" + node.getAttribute("ParamValue") + "','T_EMAIL_ATTACH_FILE','MAIL_ATTACH_FILE_ID','MAIL_ID'," + val + ")"">");
img.src = "http://www.cnblogs.com/Images/16_close.gif";
img.style.cursor = "hand";
img.title = "删除";
cell.appendChild(img);
}
}
</script>
AttachContent.aspx 页面为空 代码为:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using Gisquest.Data.Client;
using GisOANet.Business;
namespace Gisquest.OA.Email
{
/// <summary>
/// 显示附件内容
/// </summary>
public partial class AttachContent : System.Web.UI.Page
{
protected AttachFile attach = new AttachFile();
/// <summary>
/// 调用附件对象的属性加载方法,传入附件编号,读取附件内容及类型写入页面
/// </summary>
protected void Page_Load(object sender, System.EventArgs e)
{
if(!IsPostBack)
{ attach.AttachFileID = Verifier.VerifyLong(Request.QueryString.Get("AttachFileID"));
if(attach.AttachFileID != 0)
{
if(attach.Load())
{
string fileName = Verifier.VerifyString(Request.QueryString.Get("FileName")).Trim();
string storeName = attach.GetStoreName(attach.AttachFileID.ToString());
if(fileName != null && fileName != "")
{
fileName = HttpUtility.UrlEncode(fileName,System.Text.Encoding.UTF8);
Response.AppendHeader("Content-Disposition","attachment; filename=" + fileName);
}
Response.ContentType = "application/ms-download";
Response.WriteFile("http://www.cnblogs.com/AttachFile/" + storeName);
Response.End();
return;
}
}
}
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
}
#endregion
}
}
DelFile.aspx
protected void Page_Load(object sender, System.EventArgs e)
{
string table = Request.QueryString.Get("table");
string pk = Request.QueryString.Get("pk");
string fk = Request.QueryString.Get("fk");
long val = Verifier.VerifyLong( Request.QueryString.Get("val") );
string fileIds = Request.QueryString.Get("fileIds");
string[] arr = fileIds.Split(',');
AttachFile attach=new AttachFile();
for(int i=0;i<arr.Length;i++)
{
if (arr[i] == "")
continue;
attach.AttachFileID = Verifier.VerifyLong(arr[i]);
attach.AssociatedTable = table;
attach.AssociatedPrimaryKey = pk;
attach.AssociatedForeignKey = fk;
attach.ForeignKeyValue = val;
attach.Remove();
}
}
UpLoadAttachs.aspx
private void UpLoad()
{
HttpPostedFile file=Request.Files.Get("f");
if (file.ContentLength==0)
{
ClientMessageBox msgbox =new ClientMessageBox(this);
msgbox.Show("此文件不存在或为空");
return;
}
if (file.ContentLength>=20971520) //以前 是 5165760(5M,不是很明白这个数字)
{
ClientMessageBox msgbox =new ClientMessageBox(this);
msgbox.Show("允许上传文件最大为20M");
return;
}
string[] fileNames = file.FileName.Split('.');
string extenName = fileNames[fileNames.Length-1];
if( extenName.ToLower() == "docx" )
{
ClientMessageBox msgbox =new ClientMessageBox(this);
msgbox.Show("您上传的文件类型可能使得其他的用户无法打开");
return;
}
string[] word = file.FileName.Split('\\');
attach.AttachFileName = word[word.Length - 1];
attach.StoreFileName = attach.AttachFileName;
attach.AssociatedTable = Table;
attach.AssociatedPrimaryKey = PrimaryKey;
attach.AssociatedForeignKey = ForeignKey;
if(ForeignKeyValue == 0)
ForeignKeyValue = -this.User.UserId;
attach.ForeignKeyValue = ForeignKeyValue;
attach.Create();
attach.Store();
}
GetAttachs.aspx
protected void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
DataExchange exchange = new DataExchange();
try
{
exchange.Initialize(Request.InputStream);
}
catch(Exception err){
throw err;
}
attach.AssociatedTable=exchange.GetParameter("targetTable");
attach.AssociatedPrimaryKey=exchange.GetParameter("targetPrimaryKey");
attach.AssociatedForeignKey=exchange.GetParameter("targetForeignKey");
ForeignKeyValue = Verifier.VerifyLong( exchange.GetParameter("targetKeyValue") );
if(ForeignKeyValue==0)
ForeignKeyValue = - this.User.UserId;
attach.ForeignKeyValue=ForeignKeyValue;
ArrayList AttachFiels=attach.getAttachFiles();
IEnumerator iter=AttachFiels.GetEnumerator();
exchange.Clear();
XmlNode root,node,attr;
root = exchange.AddResultSegment("Attaches","");
while(iter.MoveNext())
{
attach=(AttachFile)iter.Current;
node = exchange.AddResultSegment(root,"Attach",attach.AttachFileID.ToString());
attr = root.OwnerDocument.CreateNode(XmlNodeType.Attribute,"FileName","");
attr.Value = attach.AttachFileName;
node.Attributes.SetNamedItem(attr);
}
exchange.SendResult(Response);
}