无刷新上传图片
来源:互联网 发布:深入浅出java并发包 编辑:程序博客网 时间:2024/05/16 18:19
前台html代码:
<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head> <title>无标题页</title> <script src="js/jquery-1.4.4.min.js" type="text/javascript"></script> <script type="text/javascript" language="javascript"> $(function() { $('#fileUp').change(function() { $('#uploadLog').html('开始上传中....'); $('#formFile').submit(); }); }) function uploadSuccess(msg) { if (msg.split('|').length > 1) { $('#imgShow').attr('src', msg.split('|')[1]); $('#uploadLog').html(msg.split('|')[0]); } else { $('#uploadLog').html(msg); } } </script> </head><body> <!-- 大家注意到这个form的target的了么?这个target属性的值frameFile,是form之后的iframe的name值, 这样的写法是让当前的form表单在提交表单内容的时候转交给iframe中进行页面中表单处理, 并且不会产生当前页面跳转! --> <form id='formFile' name='formFile' method="post" action="IbeaconHandler.ashx" target='frameFile' enctype="multipart/form-data"> <input type='file' id='fileUp' name='fileUp' /> <div id='uploadLog'> </div> <br /> <img width='200' src='' height='200' id='imgShow' alt='缩略图' /> </form> <!-- 这个iframe拿到post过来的表单数据后会开始在自身内部访问post过来的页面地址,在内部中它会刷新页面, 但是这已不重要了,因为当前的iframe已经被我display:none隐藏了!所以这样给用户看起来像是无刷新的 页面文件上传,其实只是做一个一个小小的技巧! --> <iframe id='frameFile' name='frameFile' style='display: none;'></iframe></body></html>
后台代码:
<%@ WebHandler Language="C#" Class="IbeaconHandler" %>using System;using System.Web;public class IbeaconHandler : IHttpHandler { public void ProcessRequest (HttpContext context) { //context.Response.ContentType = "text/plain";//需要注释了,否则会将script脚本输出,而不是调用父类的方法 //context.Response.Write("Hello World"); //string fname = context.Request.QueryString["name"].ToString(); try { //获取当前Post过来的file集合对象,在这里我只获取了<input type='file' name='fileUp'/>的文件控件 HttpPostedFile file = context.Request.Files["fileUp"]; if (file != null) { //当前文件上传的目录 //string path = context.Server.MapPath("~/images/"); string path = context.Server.MapPath("~/images/"); //当前待上传的服务端路径 string imageUrl = path + System.IO.Path.GetFileName(file.FileName); //当前文件后缀名 string ext = System.IO.Path.GetExtension(file.FileName).ToLower(); //验证文件类型是否正确 if (!ext.Equals(".gif") && !ext.Equals(".jpg") && !ext.Equals(".png") && !ext.Equals(".bmp")) { //这里window.parent.uploadSuccess()是我在前端页面中写好的javascript function,此方法主要用于输出异常和上传成功后的图片地址 context.Response.Write("<script>window.parent.uploadSuccess('你上传的文件格式不正确!上传格式有(.gif、.jpg、.png、.bmp)');</script>"); context.Response.End(); } //验证文件的大小 if (file.ContentLength > 1048576) { //这里window.parent.uploadSuccess()是我在前端页面中写好的javascript function,此方法主要用于输出异常和上传成功后的图片地址 context.Response.Write("<script>window.parent.uploadSuccess('你上传的文件不能大于1048576KB!请重新上传!');</script>"); context.Response.End(); } //开始上传 file.SaveAs(imageUrl); //这里window.parent.uploadSuccess()是我在前端页面中写好的javascript function,此方法主要用于输出异常和上传成功后的图片地址 //如果成功返回的数据是需要返回两个字符串,我在这里使用了|分隔 例: 成功信息|/Test/hello.jpg context.Response.Write("<script type=\"text/javascript\">window.parent.uploadSuccess('上传成功!|../images/" + file.FileName + "');</script>"); context.Response.End(); } else { //上传失败 context.Response.Write("上传失败!"); context.Response.End(); } } catch { //上传失败 context.Response.Write("上传失败!"); context.Response.End(); } } public bool IsReusable { get { return false; } }}
摘自:http://www.cnblogs.com/zcttxs/archive/2013/07/09/3180509.html
0 0
- 无刷新上传图片
- 图片无刷新上传
- 图片无刷新上传
- 无刷新图片上传
- 无刷新上传图片
- 上传图片 无刷新。
- 无刷新图片上传
- C# 无刷新上传图片
- Ajax无刷新上传图片
- php无刷新上传图片
- Ajax无刷新图片上传
- ajax实现无刷新上传图片
- 利用iframe实现无刷新图片上传
- 类似AJAX无刷新上传图片收藏
- 使用plupload实现无刷新上传图片
- swfupload无刷新上传图片实例教程
- JSP图片上传无刷新实现预览
- 文件_图片的无刷新上传
- 2014年3月WPF基础学习(三)
- linux各种配置
- 如何在windows下VC++使用protobuf
- cocos2dx 2.2.* 内更新资源
- ViewStateMode與EnableViewState的差別(转)
- 无刷新上传图片
- 有关QTP/UFT 12破解&无限试用
- 浅谈BigDecimal类在电子商务中至关重要的地位
- C#网络编程(异步传输字符串) - Part.3
- ios--提取图像某个点的像素值
- 用多态代替条件语句
- vvdsvds
- Ubuntu安装node.js及npm
- 使用final关键字修饰一个变量时,是引用不能变,还是引用的对象不能变?