Ejemplo n.º 1
0
        /// <summary>
        /// 保存远程文件到本地
        /// </summary>
        /// <param name="sourceUri">URI地址</param>
        /// <returns>上传后的路径</returns>
        public string RemoteSaveAs(string sourceUri)
        {
            if (!IsExternalIPAddress(sourceUri))
            {
                return("{\"status\": 0, \"msg\": \"INVALID_URL\"}");
            }
            var request = HttpWebRequest.Create(sourceUri) as HttpWebRequest;

            using (var response = request.GetResponse() as HttpWebResponse)
            {
                if (response.StatusCode != HttpStatusCode.OK)
                {
                    return("{\"status\": 0, \"msg\": \"Url returns " + response.StatusCode + ", " + response.StatusDescription + "\"}");
                }
                if (response.ContentType.IndexOf("image") == -1)
                {
                    return("{\"status\": 0, \"msg\": \"Url is not an image\"}");
                }
                try
                {
                    byte[] byteData = FileHelp.ConvertStreamToByteBuffer(response.GetResponseStream());
                    return(FileSaveAs(byteData, sourceUri, false, false));
                }
                catch (Exception e)
                {
                    return("{\"status\": 0, \"msg\": \"抓取错误:" + e.Message + "\"}");
                }
            }
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 统一保存文件
        /// </summary>
        private void FileSave(HttpContext context, HttpPostedFile upFiles, bool isWater)
        {
            if (upFiles == null)
            {
                showError(context, "请选择要上传文件!");
                return;
            }
            //检查是否允许匿名上传

            /*if (sysConfig.fileanonymous == 0 && !new ManagePage().IsAdminLogin() && !new BasePage().IsUserLogin())
             * {
             *  showError(context, "禁止匿名非法上传!");
             *  return;
             * }*/
            //获取文件信息
            string fileName = upFiles.FileName;

            byte[] byteData = FileHelp.ConvertStreamToByteBuffer(upFiles.InputStream); //获取文件流
            //开始上传
            string remsg = new UpLoad().FileSaveAs(byteData, fileName, false, isWater);
            Dictionary <string, object> dic = JSON.Deserialize <Dictionary <string, object> >(remsg);
            string status = dic["status"].ToString();
            string msg    = dic["msg"].ToString();

            if (status == "0")
            {
                showError(context, msg);
                return;
            }
            string filePath = dic["path"].ToString(); //取得上传后的路径

            showSuccess(context, fileName, filePath); //输出成功提示
        }
Ejemplo n.º 3
0
        private void UpLoadFile(HttpContext context)
        {
            Model.sysconfig sysConfig = YTS.SystemService.GlobalSystemService.GetInstance().Config.Get <Model.sysconfig>();
            //检查是否允许匿名上传

            /*if (sysConfig.fileanonymous == 0 && !new ManagePage().IsAdminLogin() && !new BasePage().IsUserLogin())
             * {
             *  context.Response.Write("{\"status\": 0, \"msg\": \"禁止匿名非法上传!\"}");
             *  return;
             * }*/

            string _delfile = DTRequest.GetString("DelFilePath");                                  //要删除的文件
            string fileName = DTRequest.GetString("name");                                         //文件名

            byte[] byteData     = FileHelp.ConvertStreamToByteBuffer(context.Request.InputStream); //获取文件流
            bool   _iswater     = false;                                                           //默认不打水印
            bool   _isthumbnail = false;                                                           //默认不生成缩略图

            if (DTRequest.GetQueryString("IsWater") == "1")
            {
                _iswater = true;
            }
            if (DTRequest.GetQueryString("IsThumbnail") == "1")
            {
                _isthumbnail = true;
            }
            if (byteData.Length == 0)
            {
                context.Response.Write("{\"status\": 0, \"msg\": \"请选择要上传文件!\"}");
                return;
            }
            UpLoad upLoad = new UpLoad();
            string msg    = upLoad.FileSaveAs(byteData, fileName, _isthumbnail, _iswater);

            //删除已存在的旧文件
            if (!string.IsNullOrEmpty(_delfile))
            {
                upLoad.DeleteFile(_delfile);
            }
            //返回成功信息
            context.Response.Write(msg);
            context.Response.End();
        }
Ejemplo n.º 4
0
        Model.sysconfig sysConfig = YTS.SystemService.GlobalSystemService.GetInstance().Config.Get <Model.sysconfig>(); //系统配置
        public void ProcessRequest(HttpContext context)
        {
            string sitepath = DTRequest.GetQueryString("site");
            int    id       = DTRequest.GetQueryInt("id");

            if (string.IsNullOrEmpty(sitepath))
            {
                context.Response.Write("出错了,站点传输参数不正确!");
                return;
            }
            //获得下载ID
            if (id == 0)
            {
                context.Response.Redirect(new Web.UI.BasePage().getlink(sitepath,
                                                                        new Web.UI.BasePage().linkurl("error", "?msg=" + Utils.UrlEncode("出错了,文件参数传值不正确!"))));
                return;
            }
            //检查下载记录是否存在
            BLL.article_attach bll = new BLL.article_attach();
            if (!bll.Exists(id))
            {
                context.Response.Redirect(new Web.UI.BasePage().getlink(sitepath,
                                                                        new Web.UI.BasePage().linkurl("error", "?msg=" + Utils.UrlEncode("出错了,您要下载的文件不存在或已经被删除!"))));
                return;
            }
            Model.article_attach model = bll.GetModel(id);
            //检查积分是否足够
            if (model.point > 0)
            {
                //检查用户是否登录
                Model.users userModel = new Web.UI.BasePage().GetUserInfo();
                if (userModel == null)
                {
                    //自动跳转URL
                    HttpContext.Current.Response.Redirect(new Web.UI.BasePage().getlink(sitepath, new Web.UI.BasePage().linkurl("login")));
                }
                //如果该用户未曾下载过该附件
                if (!bll.ExistsLog(model.id, userModel.id))
                {
                    //检查积分
                    if (model.point > userModel.point)
                    {
                        context.Response.Redirect(new Web.UI.BasePage().getlink(sitepath,
                                                                                new Web.UI.BasePage().linkurl("error", "?msg=" + Utils.UrlEncode("出错啦,您的积分不足支付本次下载!"))));
                        return;
                    }
                    //扣取积分
                    int result = new BLL.user_point_log().Add(userModel.id, userModel.user_name, model.point * -1, "下载附件:“" + model.file_name + "”,扣减积分", false);
                    //添加下载记录
                    if (result > 0)
                    {
                        bll.AddLog(userModel.id, userModel.user_name, model.id, model.file_name);
                    }
                }
            }
            //下载次数+1
            bll.UpdateField(id, "down_num=down_num+1");
            //检查文件本地还是远程
            if (model.file_path.ToLower().StartsWith("http://") || model.file_path.ToLower().StartsWith("https://"))
            {
                var request = System.Net.HttpWebRequest.Create(model.file_path) as System.Net.HttpWebRequest;
                using (var response = request.GetResponse() as System.Net.HttpWebResponse)
                {
                    if (response.StatusCode != System.Net.HttpStatusCode.OK)
                    {
                        context.Response.Redirect(new Web.UI.BasePage().getlink(sitepath,
                                                                                new Web.UI.BasePage().linkurl("error", "?msg=" + Utils.UrlEncode("出错了,您要下载的文件不存在或已经被删除!"))));
                        return;
                    }
                    byte[] byteData = FileHelp.ConvertStreamToByteBuffer(response.GetResponseStream());
                    context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");                                        //解决中文乱码
                    context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(model.file_name)); //解决中文文件名乱码
                    context.Response.AddHeader("Content-length", byteData.Length.ToString());
                    context.Response.ContentType = "application/octet-stream";
                    context.Response.BinaryWrite(byteData);
                    context.Response.Flush();
                    context.Response.End();
                }
                return;
            }
            else
            {
                //取得文件物理路径
                string fullFileName = Utils.GetMapPath(model.file_path);
                if (!File.Exists(fullFileName))
                {
                    context.Response.Redirect(new Web.UI.BasePage().getlink(sitepath,
                                                                            new Web.UI.BasePage().linkurl("error", "?msg=" + Utils.UrlEncode("出错了,您要下载的文件不存在或已经被删除!"))));
                    return;
                }
                FileInfo file = new FileInfo(fullFileName);                                                                          //路径
                context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");                                        //解决中文乱码
                context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(model.file_name)); //解决中文文件名乱码
                context.Response.AddHeader("Content-length", file.Length.ToString());
                context.Response.ContentType = "application/octet-stream";
                context.Response.WriteFile(file.FullName);
                context.Response.End();
            }
        }