由于需要 每个产品都有N张图片,并且都有性能,参数,简介, 这三个属性都需要配置一个fckeditor,也就是三个fckeditor,这样一起加载速度非常慢,上传图片,用FileUpLoad的话,每上传一次都需要刷新页面,估计你会非常痛苦,于是就把上传图片放置到页面的一个iframe里面,因为上传图片以后 用DataList显示,如果上传错了,DataList里面还有个删除,上传和删除的图片,我都放在一个DataTable里面了,用Session保存那三个fckeditor,我是根据radiobuttonlist,选中哪个,显示哪个的fckeditor,其他的visible=false;这样估计会好点,这是我上传图片的思路,无刷新上传图片,我都做了好几个例子,虚拟主机不支持,.ashax还有ajaxpro都不行,现在图片上传用iframe做好了,需要的是iframe不跟随父页面刷新,因为radiobuttonList选项变化的话,会让页面刷新的,苦恼中,查找了很多资料怎么都是子iframe刷新父iframe,郁闷,他们难道都没有遇到过 iframe不跟随父页面刷新的问题吗,我把图片信息DataTable放置在session里面,session会从iframe里面传送到主页面吗,就是iframe里面的session["dt"]和父页面的session["dt"] 一样吗 iframe怎么才不跟随父页面刷新呢,请教高手
解决方案 »
- .NET中DateTime类型的空怎么表示?
- 介绍一本Ajax方面的书籍
- 求匹配特定字符串的正则表达式!
- asp.net3.5在类库中为什么引用不到using System.Web.Security名称空间?
- 有两个网站A和B,他们共用一个sql2000的数据库 DBUser
- 超连接中使用javascript的问题???????????
- 一个关于ARRAYLIST的问题
- 怎么在asp.net下实现右键删除添加树节点的功能!???
- VS03按装问题?急!急!
- 如何用将文字转换成一张背景透明的图片,能否给源码?
- 网站被上传了木马,删不掉,com1.asp,com6.201184121630182.asp等等都删不掉,用c#删不掉?
- 两张数据表,一张有消耗量,另一张有库存,显示到gridview的时候,我添加一条消耗记录,怎么让库存相应减少?
可以用JS来控制 子页面中用 parent 可以取得父窗口
<iframe src="upload.aspx" scrolling="auto" frameborder="0" width="1000px" height="200px"></iframe>
1、可以用轻量级的编辑器
2、如果用FCKeditor可以上传图片,其它方式也可以
3、其实这个问题用AJAX的话什么都解决了
/// 上传图片 c#
/// </summary>
/// <param name= "fileNamePath "> 文件路径 </param>
/// <param name= "uriString "> 保存路径 </param>
/// using System.IO;
/// using System.Net;
private void UpLoadFile(string fileNamePath,string uriString)
{
string fileName = fileNamePath.Substring(fileNamePath.LastIndexOf( "\\ ") + 1);
string NewFileName = DateTime.Now.ToString( "yyMMddhhmmss ") + DateTime.Now.Millisecond.ToString() + fileNamePath.Substring(fileNamePath.LastIndexOf( ". "));
string fileNameExt = fileName.Substring(fileName.LastIndexOf( ". ") + 1);
if(uriString.EndsWith( "/ ") == false) uriString = uriString + "/ ";
uriString = uriString + NewFileName;
/// 创建WebClient实例
WebClient myWebClient = new WebClient();
myWebClient.Credentials = CredentialCache.DefaultCredentials;
// 要上传的文件
// FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);
FileStream fs = new FileStream(fileNamePath, FileMode.Open, FileAccess.Read);
//FileStream fs = OpenFile();
BinaryReader r = new BinaryReader(fs);
// try
// {
//使用UploadFile方法可以用下面的格式
//myWebClient.UploadFile(uriString, "PUT ",fileNamePath);
byte[] postArray = r.ReadBytes((int)fs.Length);
Stream postStream = myWebClient.OpenWrite(uriString, "PUT ");
if(postStream.CanWrite)
{
postStream.Write(postArray,0,postArray.Length);
}
else
{
Response.Write( "文件目前不可写! ");
}
postStream.Close();
} 参考
if (!IsPostBack)
{
if ((DataTable)Session["dt"] != null)
{
DataList1.DataSource = (DataTable)Session["dt"];
DataList1.DataBind();
}
}
多谢各位老大捧场,来者接奉分
<head runat="server">
<title>无标题页</title>
<style type="text/css">
body{font-size:12px;};
INPUT { BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; FONT-SIZE: 12px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px }
TEXTAREA { BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; FONT-SIZE: 12px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px }
SELECT { BORDER-TOP-WIDTH: 1px; BORDER-LEFT-WIDTH: 1px; FONT-SIZE: 12px; BORDER-BOTTOM-WIDTH: 1px; BORDER-RIGHT-WIDTH: 1px }
SPAN { FONT-SIZE: 12px; POSITION: static }
A { COLOR: #000000; TEXT-DECORATION: none }
A:hover { COLOR: #428eff; TEXT-DECORATION: underline }
Tr.forumRowHighlight { PADDING-RIGHT: 3px; PADDING-LEFT: 3px; BACKGROUND: #e4edf9; PADDING-BOTTOM: 3px; PADDING-TOP: 3px }
</style>
</head>
<body style="margin:0px; ">
<form id="form1" runat="server" >
<div style="width:150;height:30; margin:0px 0px 0px 0px; padding-right: 0px; padding-left: 0px; padding-bottom: 0px; padding-top: 0px;">
<asp:FileUpload runat="server" ID="fup1" Width="247px" />
<asp:Button ID="btn_Upload" runat="server" Text="上传" CausesValidation="false" OnClientClick="return checkfile();" OnClick="btn_Upload_Click"/>
<asp:CheckBox runat="server" ID="cbxUseOriginalName" Text="覆盖原文件" />
<asp:Label runat="server" ID="lb_Msg" Visible="False" Font-Size="Small"></asp:Label> </div>
</form>
</body>
</html>frameUpFile.aspx.csusing 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.IO;
using Galsun;
public partial class Controls_frameUpFile : adminPage
{
private string _upPath = "/uploadFile/";
//上传路径
private string _filenametype ="Random"; //保存文件名方式 private string _txtBoxUp; //上传对应的文本框
//文本框内容,返回上传的文件路径与名称
private string _OriginalName; //原文件名
protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["upPath"] != null && Request.QueryString["upPath"] != "")
{
_upPath = Request.QueryString["upPath"];
}
if (Request.QueryString["filenametype"] != null && Request.QueryString["filenametype"] != "")
{
_filenametype = Request.QueryString["filenametype"];
}
_txtBoxUp = Request.QueryString["tbId"];
_OriginalName = Request.QueryString["txtValue"];
if (_OriginalName != "") //如果有文件名,则默认覆盖。
{
cbxUseOriginalName.Checked = true;
}
string script = "<script>function checkfile()\r"
+ "{\rif(document.getElementById(\"" + fup1.ClientID + "\").value==\"\")\r"
+ "{alert(\"请选择要上传的文件!\");\r"
+ " return false;\r"
+ "}\r"
+ "return true;\r}</script>\r"; this.Page.RegisterStartupScript("sdf", script);
}
private void UploadPicFile(FileUpload Fupload)
{
//文件上传
try
{
if (Fupload.PostedFile.FileName == "")
return;
string dir = DateTime.Now.ToString("yyyyMM");
if (!Directory.Exists(Server.MapPath(_upPath + "/" + dir)))
{
Directory.CreateDirectory(Server.MapPath(_upPath + "/" + dir));
if (!Directory.Exists(Server.MapPath(_upPath + "/" + dir)))
return;
}
Random rd = new System.Random();
string filename;
string extname;
if (Fupload.PostedFile.FileName != "")
{
extname = Fupload.PostedFile.FileName.Substring(Fupload.PostedFile.FileName.LastIndexOf(".") + 1).ToUpper();
if ("DOC|MHT|RAR|ZIP|JPG|GIF|BMP|PNG".IndexOf(extname) == -1)
{
return;
}
if (cbxUseOriginalName.Checked && _OriginalName != "") //覆盖原文件
{
filename = _OriginalName;
//FSO.DeleteFile(_OriginalName);
Fupload.PostedFile.SaveAs(Server.MapPath(filename));
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "photoscript",
"parent.callBack" + _txtBoxUp + "('" + filename + "');", true);
lb_Msg.Text = "<font color='009900'>上传成功!</font>";
lb_Msg.Visible = true; }
else
{
if (_filenametype == "Random") //使用随机文件名
{
filename = dir + "/" + DateTime.Now.ToString("ddHHmmss") + "." + extname;
}
else //使用上传文件名,不支持中文
{
filename = Fupload.PostedFile.FileName.Substring(Fupload.PostedFile.FileName.LastIndexOf("/") + 1);
}
Fupload.PostedFile.SaveAs(Server.MapPath(_upPath) + filename);
Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "photoscript",
"parent.callBack" + _txtBoxUp + "('" + _upPath + filename + "');", true);
lb_Msg.Text = "<font color='009900'>上传成功!</font>";
lb_Msg.Visible = true;
}
}
}
catch(Exception e)
{
lb_Msg.Text = "<font color='red'>上传失败!</font>";
lb_Msg.Visible = true;
//throw new Exception(e.Message);
}
}
protected void btn_Upload_Click(object sender, EventArgs e)
{
if (fup1.HasFile)
UploadPicFile(fup1);
}
}adminPage是本人的权限验证代码,防止非管理员操作,如果没有相关验证可以改成System.Web.UI.Page
<script type="text/javascript" language="javascript">
var $ = function(objId) {
return document.all ? document.all[objId] : document.getElementById(objId);
}
function upShow(tbId)
{
if ($("tb_"+tbId).style.display=="none")
{
$("tb_"+tbId).style.display="inline";
}
else
{
$("tb_"+tbId).style.display="none";
}
} </script>
<div style="width:100%" >
<div style="float:left;" >
<asp:HiddenField runat="server" ID="hdId" />
<%-- <select id="upimgSelect" onchange="changeImg()">
<option value="" selected="selected">选择图片</option>
</select>--%>
<asp:TextBox ID="txt_Url" runat="server" Width="222px"></asp:TextBox>
<a href="javascript:upShow('<%#tbId %>');">[重新上传]</a>
<asp:Label ID="lb_Msg" runat="server" Text="Label" Visible="False"></asp:Label>
<span onmouseover="<%# txt_Url.ClientID%>_ShowPic('<%#tbId %>');" onmouseout="<%# txt_Url.ClientID%>_HidePic('<%#tbId %>');" style="cursor:hand;"> <img style="vertical-align:middle;" src="/Controls/icon-viewpic.gif" /></span>
</div>
<div style="float:left;" ><img id="img<%#tbId %>" style="display:none; position:absolute; width:141px; height:150px;" /></div><br />
<table id="tb_<%#tbId %>" style="DISPLAY: none" cellSpacing="0" cellPadding="0" width="99%" border="0">
<tr>
<td style="height: 21px;" >
<iframe style="height:30px;width:100%;BACKGROUND: #e4edf9;" frameborder="0" src="../Controls/frameUpFile.aspx?upPath=<%#_upPath %>&tbid=<%#tbId %>&filenametype=<%#_FileNameType %>&txtValue=<%#Text %>"></iframe>
</td>
</tr>
</table>
</div>
UploadPic.ascx.csusing 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;public partial class Controls_UploadPic : System.Web.UI.UserControl
{
protected string tbId;
protected string _upPath;
protected string _FileNameType;
//上传路径
public string upPath
{
get { return _upPath; }
set { _upPath = value; }
}
//保存文件名方式
public string FileNameType
{
get { return _FileNameType; }
set { _FileNameType = value; }
}
//文本框内容,返回上传的文件路径与名称
public string Text
{
get { return this.txt_Url.Text; }
set
{
this.txt_Url.Text = value;
this.DataBind(); //用于动态设置图片文本框时,重新绑定
}
}
protected void Page_Load(object sender, EventArgs e)
{
tbId = hdId.ClientID;
string script = "<script>function callBack" + tbId + "(filename)\r"
+ "{\rdocument.getElementById(\"" + txt_Url.ClientID + "\").value=filename;\r"
+ "setTimeout(\"upShow('" + tbId + "')\",1000);\r"
+ "}\r"
+ "</script>\r";
this.Page.RegisterStartupScript("callback" + tbId + "", script); script = "<script>function "+ txt_Url.ClientID +"_ShowPic(imgId)\r"
+ "{\rdocument.getElementById(\"img\"+imgId).src=document.getElementById(\"" + txt_Url.ClientID + "\").value;\r"
+ "\rdocument.getElementById(\"img\"+imgId).style.display=\"\";"
+ "}\r"
+ "</script>\r"; this.Page.RegisterStartupScript(txt_Url.ClientID + "_ShowPic", script);
script = "<script>function " + txt_Url.ClientID + "_HidePic(imgId)\r"
+ "{\rdocument.getElementById(\"img\"+imgId).style.display=\"none\";"
+ "}\r"
+ "</script>\r";
this.Page.RegisterStartupScript(txt_Url.ClientID+ "_HidePic", script);
this.DataBind();
}}
<%@ Register TagPrefix="uc1" TagName="UploadPic" Src="~/Controls/UploadPic.ascx" %>
....
<uc1:UploadPic id="upLoadPic1" runat="server" upPath="/uploadfile/" />
.....
.....