日期:2014-05-16  浏览次数:20393 次

js在线编辑器其中一个上传图片功能
img.html
<script language="JavaScript">function ok(){d_file.myform.submit();}
document.write('<iframe id=d_file frameborder=0 src="include/upload.aspx?type=image" width="100%" height="22" scrolling=no></iframe>');
</script>
<input type="text" id="d_fromurl" style="width: 243px" size="30" value="">
<input type="submit" value='  确定  ' id="Ok" onclick="ok()">

upload.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="upLoad.aspx.cs" Inherits="debug_htmledit_include_upLoad" %>
<script  language=javascript>
window.onload=function(){ window.parent.d_fromurl.value=document.getElementById("fileUrl").value;}
</script>
<body topmargin=0 leftmargin=0>
    <form id="myform" runat="server">
    <div>
        <input name="uploadfile" runat=server id="uploadfile" type="file" />
        <input id="fileUrl" runat=server type="hidden" />
    </div>
    </form>
</body>

upload.aspx.cs
if (Request.QueryString["type"] != null)
        {
            System.Web.HttpFileCollection uploadFiles = Request.Files;
            System.Web.HttpPostedFile theFile;
            for (int i = 0; i < uploadFiles.Count; i++)
            {///5/a/a/spx
                theFile = uploadFiles[i];
                string filename = theFile.FileName.Substring(theFile.FileName.LastIndexOf('\\') + 1);

                Random seed = new Random(10000);
                int fname1 = filename.Length;
                int fname2 = filename.LastIndexOf('.');
                filename = seed.Next() + seed.Next() + filename.Substring(fname2).ToString();

                string filepath = Server.MapPath("../uploadfiles/" + filename);
                theFile.SaveAs(filepath);

                this.fileUrl.Value = filename;
            }
        }