很简单。
答案是你使用的ASPX页面会自动POSTBACK.
只要在这个upfile.aspx的页面上加上<base语句就可以了。
<html>
<head>
....
<base target="_self">
<body>
....
</html>
源代码这样的,这个程序是一个HTMLEDIT。节选部分单元。
// Custom1(弹出命令)
else
if (cmdID == 'upfile') {//提出警告例子
var myTitle="上传图像";
var myText=window.showModalDialog(_editor_url+"popups/upfile.aspx",
myTitle,
"resizable: yes;
help: no;
status: no;
scroll: no;"
;
if (myText+"xxxx"
{ editor_insertHTML(objname,myText);}
//alert("你好,我是一个自定义按钮"
;
}
//----弹出upfile.aspx
<%@ Page Language="C#"
AutoEventWireup="true"
CodeFile="upfile.aspx.cs"
Inherits="include_popups_upfile"
%>
<!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"
style="width:360px;
Height: 60px;">
<head runat="server">
<base target="_self"
/>
<title>上传图片</title>
<link href="../../include/popups/css.css"
rel="stylesheet"
type="text/css"
/>
<script language="javascript"
type="text/javascript">
// <!CDATA[
function btnCancle_onclick() {
window.close();
}
// ]]>
</script>
</head>
<body>
<form id="form1"
runat="server">
<div>
<table style="width: 415px">
<tr>
<td style="width: 76px;
height: 26px;
text-align: right">
<span style="font-size: 10pt">图片文件:</span></td>
<td style="width: 107px;
height: 26px">
<asp:FileUpload ID="FileUpload1"
runat="server"
CssClass="button2"
/>
</td>
</tr>
<tr>
<td colspan="2"
style="height: 25px;
text-align: center;
background-color: #ccccff;">
<asp:Button ID="btnOk"
runat="server"
Text="上传"
Width="69px"
OnClick="btnOk_Click"
CssClass="button2"
/>
<input id="btnCancle"
style="width: 65px"
type="button"
value="关闭"
onclick="return btnCancle_onclick()"
class="button2"
/></td>
</tr>
</table>
</div>
</form>
</body>
</html>
-----upfile.aspx.cs程序。
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
using System.IO;
using System.Text;
public partial class include_popups_upfile : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (Session["用户"] == null)
{
Response.Write("<script>window.close();</script>"
;
}
}
protected void btnOk_Click(object sender, EventArgs e)
{
string FileExt = (Path.GetExtension(FileUpload1.FileName).ToLower());
if (".jpg"
!= FileExt && ".jpeg"
!= FileExt)
{
Response.Write("<script language='javascript'>alert('对不起,你必须选择jpg图像文件!')</script>"
;
return;
}
if (Session["用户"] == null)
{
return;
}
string connstr = System.Configuration.ConfigurationManager.AppSettings["xaHome"];
SqlConnection conn = new SqlConnection(connstr);
SqlCommand qdml = new SqlCommand();
qdml.Connection = conn;
qdml.CommandType = CommandType.StoredProcedure;
qdml.CommandText = "spAppendFile";
//-----参数赋值-----
qdml.Parameters.Add("@pUser", SqlDbType.VarChar).Value = Convert.ToString(Session["用户"]);
qdml.Parameters.Add("@pFile", SqlDbType.VarChar).Value = FileExt;
SqlParameter ret = qdml.Parameters.Add("ret", SqlDbType.Int);
ret.Direction = ParameterDirection.ReturnValue;//返回值
qdml.Connection.Open();
qdml.ExecuteNonQuery();
//----开始保存图像----
if (null != FileExt)
{
string filename = Server.MapPath("~"
+ "//files//"
+ Convert.ToString(ret.Value) + FileExt;
FileUpload1.SaveAs(filename);
//----保存并且,返回确定对话框----
//string sImg = Convert.ToString(ret.Value) + FileExt;
string sImg = "<img src=files////"+Convert.ToString(ret.Value)+FileExt+">";
StringBuilder sbScript = new StringBuilder();
sbScript.Append("<script>/n"
;
sbScript.Append("window.returnValue=/""
+ sImg + "/";/n"
;
sbScript.Append("window.close();/n"
;
sbScript.Append("</script>/n"
;
string script = sbScript.ToString();
if (!ClientScript.IsClientScriptBlockRegistered("clientScript"
)
{
ClientScript.RegisterClientScriptBlock(this.GetType(), "clientscript", script);
// RegisterClientScriptBlock("clientScript", script);过时
}
}
}
protected void btnOk_Command(object sender, CommandEventArgs e)
{
}
}