public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string userid = context.Request.Params["userid"].ToString();

            SchWebServ.UserApi.DataRsp rsp = new SchWebServ.UserApi.DataRsp();
            if (string.IsNullOrEmpty(userid))
            {
                context.Response.Write("非法链接!");
                context.Response.End();
            }
            try
            {
                HttpPostedFile file = context.Request.Files[0];
                if (file != null)
                {
                    string oldFileName = file.FileName;                                                     //原文件名
                    int    size        = file.ContentLength;                                                //附件大小
                    string extenstion  = oldFileName.Substring(oldFileName.LastIndexOf(".") + 1).ToLower(); //后缀名
                    //根据后缀名判断文件类型,并控制文件大小
                    if (size > 1024 * 1024 * 2)
                    {
                        rsp.RspCode = "0011";
                        rsp.RspTxt  = "文件不能大于2M";
                    }
                    else
                    {
                        if (extenstion.IndexOf("jpg") > -1 || extenstion.IndexOf("jpeg") > -1 || extenstion.IndexOf("png") > -1 || extenstion.IndexOf("gif") > -1)
                        {
                            string newFileName = GetNewFileName(oldFileName, userid);//生成新文件名
                            //LogTextHelper.Info(newFileName);
                            #region   到远程服务器
                            //FileServerManage fsw = new FileServerManage();
                            //string uploadFilePath = "/" + newFileName;
                            //if (!string.IsNullOrEmpty(folder))
                            //{
                            //    uploadFilePath = string.Format("/{0}/{1}", folder, newFileName);
                            //}
                            //bool uploaded = fsw.UploadFile(file.InputStream, "/" + folder + "/" + newFileName);
                            #endregion
                            #region 本地服务器上传
                            //AppConfig config = new AppConfig();
                            string uploadFiles = "";// config.AppConfigGet("uploadFiles");
                            if (string.IsNullOrEmpty(uploadFiles))
                            {
                                uploadFiles = "UploadFileDir";
                            }
                            string uploadPath = Path.Combine(HttpContext.Current.Server.MapPath("~/"), uploadFiles, userid);
                            if (!Directory.Exists(uploadPath))
                            {
                                Directory.CreateDirectory(uploadPath);
                            }
                            ;
                            string fileurl     = context.Request.Url.Authority + context.Request.ApplicationPath + "/" + uploadFiles + "/" + userid + "/" + newFileName;
                            string newFilePath = Path.Combine(uploadPath, newFileName);
                            //LogTextHelper.Info(newFilePath);
                            file.SaveAs(newFilePath);
                            bool uploaded = File.Exists(newFilePath);
                            #endregion
                            if (uploaded)
                            {
                                SchSystem.BLL.SchUserInfo suiBll = new SchSystem.BLL.SchUserInfo();
                                suiBll.UploadPicture(int.Parse(userid), Path.Combine(uploadFiles, userid, newFileName));
                                rsp.RspData = fileurl;

                                #region 文件保存成功后,写入附件的数据库记录

                                #endregion
                            }
                        }
                        else
                        {
                            rsp.RspCode = "0011";
                            rsp.RspTxt  = "文件格式不符合要求,请上传jpg,jpeg,png,gif文件";
                        }
                    }
                }
                else
                {
                    rsp.RspCode = "0011";
                    rsp.RspTxt  = "上传文件失败";
                    //LogTextHelper.Error("上传文件失败");
                }
            }
            catch (Exception ex)
            {
                rsp.RspCode = "0011";
                rsp.RspTxt  = "上传文件失败:" + ex.Message;
            }
            finally
            {
                context.Response.Write(JsonConvert.SerializeObject(rsp));
            }
        }
Пример #2
0
        public void ProcessRequest(HttpContext context)
        {
            string msg         = string.Empty;
            string error       = string.Empty;
            string fileNewName = string.Empty;
            string result      = string.Empty;
            bool   resBool     = false;

            Com.DataPack.DataRsp <Com.DataPack.UserInfo> rsp = Com.Public.UserFuncSoure(Com.SoureSession.jsid, Com.SoureSession.jstoken);
            if (rsp.code == "ERROR_TOKEN")
            {
                error  = "登录失效!";
                result = "{ error:'登录失效',status:'error'}";
            }
            else
            {
                context.Response.ContentType = "text/plain";
                HttpFileCollection files = HttpContext.Current.Request.Files;
                SchManagerInfoSystem.Common.UploadFile uploadf = new SchManagerInfoSystem.Common.UploadFile();

                uploadf.Path     = "UploadFileDir\\Users"; //设置上传文件路径
                uploadf.FileType = "jpg|gif|bmp|jpeg|png"; //设置上传文件格式
                uploadf.Sizes    = 100;                    //设置上传文件的大小,默认100KB


                if (files.Count > 0)
                {
                    fileNewName = uploadf.SaveAs(files);//保存上传文件到服务器
                    if (fileNewName == "0")
                    {
                        error  = "文件上传失败!";
                        result = "{ error:'上传的图片不能大于100KB',status:'success01'}";
                    }
                    else if (fileNewName == "1")
                    {
                        error  = "文件上传失败!";
                        result = "{ error:'出现未知错误',status:'success02'}";
                    }
                    else
                    {
                        SchSystem.BLL.SchUserInfo suiBll = new SchSystem.BLL.SchUserInfo();
                        try
                        {
                            resBool = suiBll.UploadPicture(int.Parse(Com.SoureSession.Soureusertid), int.Parse(Com.SoureSession.Soureschid), fileNewName);
                            Com.SoureSession.Soureimgurl = fileNewName;
                        }
                        catch (Exception e)
                        {
                            error = e.Message;
                        }
                        if (resBool)
                        {
                            msg    = "文件上传成功!";
                            result = "{msg:\"" + msg + "\",filenewname:\"" + fileNewName.Replace("\\", "/") + "\"}";
                        }
                        else
                        {
                            error  = "文件上传失败!";
                            result = "{ error:'" + error + "'}";
                        }
                    }
                }
                else
                {
                    error  = "文件上传失败!";
                    result = "{ error:'" + error + "'}";
                }
            }
            context.Response.Write(result);
            context.Response.End();
        }