C# 无刷新上传图片

来源:互联网 发布:90后网络语言 编辑:程序博客网 时间:2024/05/01 19:55

<style type="text/css">        .btn        {            width: 50px;            height: 25px;            line-height: 25px;        }        .yc        {            display: none;        }    </style><div style="text-align: center; width:150px;">        <input type="button" class="btn" onclick="javascript: ck();" value="浏览" />        <asp:FileUpload ID="FileUpload1" onchange="onUpFile(this.value,this)" runat="server"            CssClass="yc" />        <asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" CssClass="btn" />    </div>   

实现思路 父级页面 嵌套 iframe 通过 iframe 上传 把保存后的文件路径 返回给父级页面


父级页面关键代码

<asp:Image ID="imgProductImage" runat="server" Width="300px" /><iframe id="imgUpload" frameborder="0" height="40px" name="I1" scrolling="no" src="" style="width: 170px; border: 0px;"></iframe>

<script type="text/javascript">    $("#imgUpload").attr("src", "PicUpload.aspx");    function updateImgs(filename) {        var fullName = "../../upload/" + filename;        $("#imgProductImage").attr("src", fullName);        $("#txtFilePath").val("upload/" + filename);    }</script>   

 子级页面后台代码

 protected void Button1_Click(object sender, EventArgs e)        {            string path = Server.MapPath("~/upload/");            if (!Directory.Exists(path))            {                Directory.CreateDirectory(path);            }            if (FileUpload1.HasFile)            {                long truelength = FileUpload1.PostedFile.ContentLength;//文件长度(以字节为单位)。                int Mylen = 4;//4M                                   long len = 0;                try                {                    len = Mylen * 1024 * 1024;                }                catch                {                    len = Convert.ToInt32(1024 * 1024 * 4);                }                if (truelength > len)                {                    ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('文件大于4M上传失败!')</script>");                }                string fullFileName = FileUpload1.PostedFile.FileName;                string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + fullFileName.Substring(fullFileName.LastIndexOf("\\") + 1);   //图片文件名称                FileUpload1.SaveAs(path + fileName);//存储文件到磁盘                  //Image1.ImageUrl = "UpDataFile/Face/" + fileName;                //HiddenField2.Value = fileName;                string str = "function uploadok(fileName) {parent.updateImgs(fileName); }";                Response.Write("<script>" + str + "uploadok('" + fileName + "');</script>");            }            else            {                ClientScript.RegisterStartupScript(this.GetType(), "msg", "<script>alert('上传失败, 请选择图片!')</script>");            }        }
子级页面前台代码

 <script type="text/javascript">        $(function () {            $("#Button1").attr("disabled", true);        });        function onUpFile(file, obj) {            var ExtendName = file.substring(file.lastIndexOf(".") + 1).toLowerCase();            if (ExtendName == "jpg" || ExtendName == "png" || ExtendName == "jpeg") {                $("#Button1").removeAttr("disabled");            }            else {                obj.value = "";                alert('附件格式只能是jpg,png,jpeg不支持其他格式!');                return;            }        }    </script>


 <script type="text/javascript">        function ck() {            document.getElementById('FileUpload1').click();        }    </script>




0 0
原创粉丝点击