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
- C# 无刷新上传图片
- 无刷新上传图片
- 图片无刷新上传
- 图片无刷新上传
- 无刷新图片上传
- 无刷新上传图片
- 上传图片 无刷新。
- 无刷新图片上传
- Ajax无刷新上传图片
- php无刷新上传图片
- Ajax无刷新图片上传
- ajax实现无刷新上传图片
- 利用iframe实现无刷新图片上传
- 类似AJAX无刷新上传图片收藏
- 使用plupload实现无刷新上传图片
- swfupload无刷新上传图片实例教程
- JSP图片上传无刷新实现预览
- 文件_图片的无刷新上传
- 使用CXF开发RestFul WebService问题解决方案
- 使用baseadapter控制listview内的item可用状态
- Freesurfer Uesage
- Java虚拟机解析篇之---垃圾回收器
- iOS plist文件的读写
- C# 无刷新上传图片
- Python学习笔记(一)
- java利用google zxing 生成二维码
- 第一次玩微博哦~好嗨森!
- 苹果信息推送服务(Apple Push Notification Service)使用总结
- doc转html
- 事务传播行为种类
- 北奔6X6洒水车操作注意事项
- 把小米 3 添加到 ADB 支持设备列表