Ejemplo n.º 1
0
    public Crawler Fetch()
    {
        var request = HttpWebRequest.Create(this.SourceUrl) as HttpWebRequest;

        using (var response = request.GetResponse() as HttpWebResponse)
        {
            if (response.StatusCode != HttpStatusCode.OK)
            {
                State = "Url returns " + response.StatusCode + ", " + response.StatusDescription;
                return(this);
            }
            if (response.ContentType.IndexOf("image") == -1)
            {
                State = "Url is not an image";
                return(this);
            }
            //Delete By Arvin 20140811 粘贴图片改为接口上传
            //ServerUrl = PathFormatter.Format(Path.GetFileName(this.SourceUrl), Config.GetString("catcherPathFormat"));
            //var savePath = Server.MapPath(ServerUrl);
            //if (!Directory.Exists(Path.GetDirectoryName(savePath)))
            //{
            //    Directory.CreateDirectory(Path.GetDirectoryName(savePath));
            //}
            //Delete End
            try
            {
                var    stream = response.GetResponseStream();
                var    reader = new BinaryReader(stream);
                byte[] bytes;
                using (var ms = new MemoryStream())
                {
                    byte[] buffer = new byte[4096];
                    int    count;
                    while ((count = reader.Read(buffer, 0, buffer.Length)) != 0)
                    {
                        ms.Write(buffer, 0, count);
                    }
                    bytes = ms.ToArray();
                }
                //Modify By Arvin 20140811 粘贴图片改为接口上传
                //File.WriteAllBytes(savePath, bytes);
                //获取用户所在公司ID
                UserCenter.Models.Model.SPUserCookie user = GlobalFuncs.GetUserInfoObj();
                if (user != null)
                {
                    ServerUrl = GlobalFuncs.CreatePicClientObj().SaveNewsImg(bytes, user.ComId);//新闻接口上传图片
                }
                //Modify End
                State = "SUCCESS";
            }
            catch (Exception e)
            {
                State = "抓取错误:" + e.Message;
            }
            return(this);
        }
    }
Ejemplo n.º 2
0
    public override void Process()
    {
        byte[] uploadFileBytes = null;
        string uploadFileName  = null;

        if (UploadConfig.Base64)
        {
            uploadFileName  = UploadConfig.Base64Filename;
            uploadFileBytes = Convert.FromBase64String(Request[UploadConfig.UploadFieldName]);
        }
        else
        {
            var file = Request.Files[UploadConfig.UploadFieldName];
            uploadFileName = file.FileName;

            if (!CheckFileType(uploadFileName))
            {
                Result.State = UploadState.TypeNotAllow;
                WriteResult();
                return;
            }
            if (!CheckFileSize(file.ContentLength))
            {
                Result.State = UploadState.SizeLimitExceed;
                WriteResult();
                return;
            }

            uploadFileBytes = new byte[file.ContentLength];
            try
            {
                file.InputStream.Read(uploadFileBytes, 0, file.ContentLength);
            }
            catch (Exception)
            {
                Result.State = UploadState.NetworkError;
                WriteResult();
            }
        }

        Result.OriginFileName = uploadFileName;
        try
        {
            //Modify By Arvin 20140805 本地存储改为接口上传
            //var savePath = PathFormatter.Format(uploadFileName, UploadConfig.PathFormat);
            //var localPath = Server.MapPath(savePath);
            //if (!Directory.Exists(Path.GetDirectoryName(localPath)))
            //{
            //    Directory.CreateDirectory(Path.GetDirectoryName(localPath));
            //}
            //File.WriteAllBytes(localPath, uploadFileBytes);
            //Result.Url = savePath;


            string   uploadImgParams = Request.QueryString["UploadImgParams"] ?? ""; //此参数代表上传图片的参数用逗号分割,第一个参数代表从那个功能页面上传的图片
            string[] param           = null;                                         //上传图片参数
            if (string.IsNullOrWhiteSpace(uploadImgParams))
            {
                //如果没设置此参数,默认设置第一个参数,为提交新闻页
                param = new string[1] {
                    "news"
                };
            }
            else
            {
                param = uploadImgParams.Split(',');
            }
            string savePath = "";
            //获取用户所在公司ID
            UserCenter.Models.Model.SPUserCookie user = GlobalFuncs.GetUserInfoObj();
            if (user != null)
            {
                if (UploadConfig.UploadFieldName == "upfiles")
                {
                    savePath = GlobalFuncs.CreatePicClientObj().SaveFile(uploadFileBytes, user.ComId, Path.GetExtension(Result.OriginFileName));
                }
                else
                {
                    switch (param[0].ToLower())
                    {
                    case "news":
                        savePath = GlobalFuncs.CreatePicClientObj().SaveNewsImg(uploadFileBytes, user.ComId);    //新闻上传图片
                        break;

                    case "teacherroom":
                        if (param.Length == 2)
                        {
                            //直播室页面上传图片至少有2个参数
                            int roomId = Convert.ToInt32(param[1]);
                            savePath = GlobalFuncs.CreatePicClientObj().SaveRoomPic(uploadFileBytes, user.ComId, roomId);    //直播室上传图片
                        }
                        break;

                    default:
                        savePath = GlobalFuncs.CreatePicClientObj().SaveNewsImg(uploadFileBytes, user.ComId);    //默认为新闻上传图片
                        break;
                    }
                }
                if (string.IsNullOrWhiteSpace(savePath))
                {
                    Result.State = UploadState.FileAccessError;
                }
                else
                {
                    Result.State = UploadState.Success;
                }
            }
            else
            {
                Result.State = UploadState.Unknown;//如果没登录就报未知错误
            }
            Result.Url = savePath;
            //Modify End
        }
        catch (Exception e)
        {
            Result.State        = UploadState.FileAccessError;
            Result.ErrorMessage = e.Message;
        }
        finally
        {
            WriteResult();
        }
    }