ASP.NET 跨域上传图片


 

 <script type="text/javascript" src="/uploadify/swfobject.js"></script>
  <script type="text/javascript" src="/uploadify/jquery.uploadify.v2.1.4.min.js"></script>

  我的资源里面有插件

///加载
                function uploadP() {
                    var jsonImg = { "big": [], "small": [] };
                    $("#txt_pic").uploadify({
                        'uploader': '/uploadify/uploadify.swf',
                        'script': 'http://localhost:98/UpLoad/SaftyPic.ashx' + "?" + Math.random() * 10000, //
                        'cancelImg': '/uploadify/cancel.png',
                        'scriptData': { 'opType': 'addImg', 'ID': '<%=id%>', 'uu': '<%=uu%>' },
                        'auto': true,
                        'multi': false,
                        'width': 20,
                        'height': 20,
                        'fileDesc': '图片选择',
                        'hideButton ': true,
                        'fileExt': '*.jpg;*.png;*.gif;*.bmp;*.jpeg',
                        'queueSizeLimit': 10,
                        'sizeLimit': 4000000,
                        'buttonImg': '/images/tb.gif',
                        'folder': '/uploads',
                        //选择文件后向队列中添加每个上传任务时都会触发。
                        'onSelect': function (e, queueId, fileObj) {
                        },
                        'onOpen': function (e, queueId, fileObj) {
                        },
                        'onError': function (e, queueId, fileObj, errorObj) {
                            if (errorObj.type == 'HTTP') {
                                alert('加载失败,未找到存储页面');
                            }
                        },
                        'onComplete': function (event, queueID, fileObj, response, data) {
                            if (response == "0") {
                                alert("失败!");
                            }
                            if (response == "1") {
                                parent.LoadGrid();
                            }
                        },
                        'onAllComplete': function (event, data) {
                            location.href = location.href;
                            //$("#img" + id).attr("src", "<%=url%>.jpg?id=" + RndNum(8));
                        }
                    });
                }
                ///删除
                function delImg(id,uu) {
                    $.ajax({
                        type: 'POST',
                        url: "http://localhost:98/UpLoad/SaftyPic.ashx",
                        data: { 'opType': 'delImg', "id": id, "uu": uu },
                        success: function (msg) {
                            location.reload([true]);
                            parent.LoadGrid();
                            if (msg == "1") {
                                Shows("温馨提示", "成功!", "slide", 5000);
                            }
                        }
                    })
                    location.reload([true]);
                }

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、、

    /// <summary>
    /// SaftyPic 的摘要说明
    /// </summary>
    public class SaftyPic : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            context.Request.ContentEncoding = Encoding.GetEncoding("UTF-8");
            string opType = Decode("opType"), html = "0";
                switch (opType)
                {
                    case "addImg":
                        html = addImg();//增加图片
                        break;
                    case "delImg":
                        html = delImg();//得到图片
                        break;
            }
            context.Response.Write(html);
            context.Response.End();
        }
        /// <summary>
        /// 删除图片
        /// </summary>
        /// <returns></returns>
        private string delImg()
        {
            string uu = Decode("uu");
            string rs = "0", id = Decode("id");
            //string jsonp = Decode("callback");
                try
                {
                    string img = HttpContext.Current.Server.MapPath("~/bizpic/safety/");
                    string[] idList = id.Split(',');
                    rs = "";
                    for (int i = 0; i < idList.Length; i++)
                    {
                        Hashtable hs = new Hashtable();
                        classDBOP dbop = new classDBOP();
                        if (!ClassStrBase.IsNum2(idList[i]))
                        {
                            return "";
                        }
                        hs.Add("LogUrl", "");

                         ///数据库操作
                        new classDBOP().Update(hs, "Safety_Com", "id=" + idList[i], 1);
                    }
                    string[] uuList = uu.Split(',');
                    for (int i = 0; i < uuList.Length; i++)
                    {
                        string iPath = img + uuList[i] + "_b.jpg";
                        FileInfo fi = new FileInfo(iPath);
                        if (fi.Exists)
                        {
                            fi.Delete();
                        }
                        iPath = img + uuList[i] + "_s.jpg";
                        fi = new FileInfo(iPath);
                        if (fi.Exists)
                        {
                            fi.Delete();
                            rs = "1";
                        }
                    }
                }
                catch (Exception ex)
                {
                    ClassAppLogs.WriteLogToLocalTextstatic(ex);
                }
            return rs;
        }
        /// <summary>
        /// 添加图片
        /// </summary>
        /// <returns></returns>
        private string addImg()
        {
            string id = Decode("ID");
            string uu = Decode("uu");
           
            if (!ClassStrBase.IsNum2(id))
            {
                return "0";
            }
            ////原来以为这里的FILES只是file文件上传才能取得,后来经测试发现这里的files
            ////只要是页面上有的file文件(图片等等),都会当做request.files传过去
            foreach (string fileKey in HttpContext.Current.Request.Files)
            {
                /// HttpInputFile是继承HttpPostedFile,HttpInputFile多了个PostedFile
                /// 来访问上传的文件对象。而HttpPostedFile是直接就是该文件对象。
                HttpPostedFile file = HttpContext.Current.Request.Files[fileKey];
                if (file.ContentLength > 0)
                {
                    ClassUpImg upimg = new ClassUpImg();
                    string fPath = "bizpic/safety/";
                    string rs = upimg.UpLoadImg("", fPath, uu+"_b", uu +"_s", ".jpg", 0, "", 350, 200, file);
                    if (rs == "-1")
                    {
                       classDBOP dbop = new classDBOP();
                        Hashtable hs = new Hashtable();
                        string ImagWeb = "http://localhost:98/bizpic/safety/";
                        ///string ImagWeb = System.Configuration.ConfigurationManager.AppSettings["url"];
                        ///服务器路径+大图+小图
                        hs.Add("LogUrl", ImagWeb + "@" + uu + "_b.jpg@" + "" + uu + "_s.jpg");
                        if (dbop.Update(hs, "Safety_Com", "id=" + id, 1) > 0)
                        {
                            return "1";
                        }
                    }
                }
                break;
            }
            return "0";
        }
        //转换参数
        private string Decode(string str)
        {
            string rs = "";
            if (HttpContext.Current.Request[str] != null)
            {
                rs = HttpContext.Current.Request[str].ToString().Trim();
                rs = HttpUtility.UrlDecode(rs).Trim();
            }
            return rs;
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

UpLoadImg+CreateFolder在资源里面,不要豆豆

 

智能推荐

注意!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系我们删除。



 
© 2014-2019 ITdaan.com 粤ICP备14056181号  

赞助商广告