Example #1
0
        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="VirtualPath"></param>
        /// <param name="file"></param>
        /// <param name="extension"></param>
        /// <returns></returns>
        public static OperationResult <string> UpLoadFile(string VirtualPath, HttpPostedFileBase file, string extension)
        {
            try
            {
                var fileContent = default(byte[]);
                var ism         = file.InputStream;
                using (var ms = new MemoryStream())
                {
                    ism.CopyTo(ms);
                    ms.Flush();
                    fileContent = ms.ToArray();
                }

                using (Tuhu.Service.Utility.FileUploadClient fileUploadClient = new Tuhu.Service.Utility.FileUploadClient())
                {
                    var fileUploadRequest = new FileUploadRequest
                    {
                        Contents      = fileContent,
                        DirectoryName = VirtualPath,
                        Extension     = "." + extension
                    };
                    var result = fileUploadClient.UploadFile(fileUploadRequest);
                    return(result);
                }
            }
            catch (Exception ex)
            {
                throw;
            }
        }
        public Tuple <string, string> UploadFile(byte[] buffer, FileType type, string extension, string fileName, string user)
        {
            var result       = string.Empty;
            var uploadDomain = "/UploadFile/File";
            var batchCode    = string.Empty;

            try
            {
                if (buffer.Count() > 0)
                {
                    using (var client = new Tuhu.Service.Utility.FileUploadClient())
                    {
                        var getResult = client.UploadFile(new Service.Utility.Request.FileUploadRequest()
                        {
                            Contents      = buffer,
                            DirectoryName = uploadDomain,
                            Extension     = extension
                        });
                        getResult.ThrowIfException(true);
                        if (getResult.Success && getResult.Exception == null)
                        {
                            result    = getResult.Result;
                            buffer    = null;
                            batchCode = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                            LoggerManager.InserUploadFiletLog(result, type, fileName, batchCode, user);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
            return(Tuple.Create(result, batchCode));
        }
Example #3
0
        private string ImageUploadToAli(byte[] buffer)
        {
            string    _BImage  = string.Empty;
            string    _SImage  = string.Empty;
            string    _ImgGuid = Guid.NewGuid().ToString();
            Exception ex       = null;

            if (buffer.Count() > 0)
            {
                try
                {
                    using (var client = new Tuhu.Service.Utility.FileUploadClient())
                    {
                        string dirName = System.Web.Configuration.WebConfigurationManager.AppSettings["UploadDoMain_image"];
                        var    result  = client.UploadImage(new ImageUploadRequest(dirName, buffer));
                        result.ThrowIfException(true);
                        if (result.Success)
                        {
                            _BImage = result.Result;
                            //_SImage= ImageHelper.GetImageUrl(result.Result, 100);
                        }
                    }
                }
                catch (Exception error)
                {
                    ex = error;
                }
            }
            string imgUrl = WebConfigurationManager.AppSettings["DoMain_image"] + _BImage;

            return(imgUrl);
        }
Example #4
0
        public ActionResult UploadFileByZip()
        {
            Dictionary <string, object> dic = new Dictionary <string, object>();

            if (Request.Files.Count > 0)
            {
                var file      = Request.Files[0];
                var extension = file.FileName.Substring(file.FileName.LastIndexOf('.'), 4);
                if (extension.ToLower() != ".zip")
                {
                    dic.Add("Code", 0);
                    dic.Add("Msg", "请上传.zip格式的文件");
                    return(Content(JsonConvert.SerializeObject(dic)));
                }

                var buffer = new byte[file.ContentLength];
                file.InputStream.Read(buffer, 0, buffer.Length);
                using (var client = new Tuhu.Service.Utility.FileUploadClient())
                {
                    var result = client.UploadFile(new Service.Utility.Request.FileUploadRequest()
                    {
                        Contents      = buffer,
                        DirectoryName = ConfigurationManager.AppSettings["UploadDoMain_file"].ToString(),
                        Extension     = ".zip"
                    });
                    result.ThrowIfException(true);
                    if (result.Success)
                    {
                        dic.Add("Code", 1);
                        dic.Add("Zip", ConfigurationManager.AppSettings["DoMain_image"].ToString() + result.Result);
                        dic.Add("Md5", UtilityHelper.GetMD5HashFromFile(buffer));
                        buffer = null;
                        return(Content(JsonConvert.SerializeObject(dic)));
                    }
                    else
                    {
                        dic.Add("Code", 0);
                        dic.Add("Exception", result.Exception);
                        dic.Add("Msg", "上传失败");
                        return(Content(JsonConvert.SerializeObject(dic)));
                    }
                }
            }
            else
            {
                dic.Add("Code", 0);
                dic.Add("Msg", "请选择要上传的zip文件");
            }
            return(Content(""));
        }
Example #5
0
        public ActionResult UploadFileByApi()
        {
            Dictionary <string, object> dic = new Dictionary <string, object>();

            if (Request.Files.Count > 0)
            {
                var file      = Request.Files[0];
                var extension = file.FileName.Substring(file.FileName.LastIndexOf('.'), 4);
                var buffer    = new byte[file.ContentLength];
                file.InputStream.Read(buffer, 0, buffer.Length);
                using (var client = new Tuhu.Service.Utility.FileUploadClient())
                {
                    var result = client.UploadFile(new Service.Utility.Request.FileUploadRequest()
                    {
                        Contents      = buffer,
                        DirectoryName = ConfigurationManager.AppSettings["UploadDoMain_pdf"].ToString(),
                        Extension     = extension.ToLower()
                    });
                    result.ThrowIfException(true);
                    if (result.Success)
                    {
                        LoggerManager.InsertOplog(new ConfigHistory()
                        {
                            AfterValue = result.Result, ChangeDatetime = DateTime.Now, ObjectType = "FileUp", Operation = "上传文件记录", Author = User.Identity.Name
                        });
                        dic.Add("Code", 1);
                        dic.Add("File", ConfigurationManager.AppSettings["DoMain_image"].ToString() + result.Result);
                        dic.Add("Md5", UtilityHelper.GetMD5HashFromFile(buffer));
                        buffer = null;
                        return(Content(JsonConvert.SerializeObject(dic)));
                    }
                    else
                    {
                        dic.Add("Code", 0);
                        dic.Add("Exception", result.Exception);
                        dic.Add("Msg", "上传失败");
                        return(Content(JsonConvert.SerializeObject(dic)));
                    }
                }
            }
            else
            {
                dic.Add("Code", 0);
                dic.Add("Msg", "请选择要上传的文件");
            }
            return(Content(""));
        }
Example #6
0
        /// <summary>
        /// 图片上传地址
        /// </summary>
        /// <param name="maxWidth">最大宽度</param>
        /// <param name="maxHeight">最大高度</param>
        /// <param name="identifying">标识</param>
        /// <param name="urlStr">地址</param>
        /// <returns></returns>
        public ActionResult ImageUploadToAli(string from)
        {
            string    _BImage  = string.Empty;
            string    _SImage  = string.Empty;
            string    _ImgGuid = Guid.NewGuid().ToString();
            Exception ex       = null;

            if (Request.Files.Count > 0)
            {
                var Imgfile = Request.Files[0];
                try
                {
                    var buffer = new byte[Imgfile.ContentLength];
                    Imgfile.InputStream.Read(buffer, 0, buffer.Length);
                    using (var client = new Tuhu.Service.Utility.FileUploadClient())
                    {
                        string dirName = swc.WebConfigurationManager.AppSettings["UploadDoMain_image"];

                        var result = client.UploadImage(new ImageUploadRequest(dirName, buffer));
                        result.ThrowIfException(true);
                        if (result.Success)
                        {
                            _BImage = result.Result;
                            //_SImage= ImageHelper.GetImageUrl(result.Result, 100);
                        }
                    }
                }
                catch (Exception error)
                {
                    ex = error;
                }
            }
            string imgUrl = swc.WebConfigurationManager.AppSettings["DoMain_image"] + _BImage;

            if (from == "content")
            {
                return(Json(new { error = 0, url = imgUrl }));
            }
            return(Json(new
            {
                BImage = _BImage,
                SImage = imgUrl,
                Msg = ex == null ? "上传成功" : ex.Message
            }, "text/html"));
        }
        public static Tuple <bool, string> UpdateLoadImage(this byte[] buffer)
        {
            using (var client = new Tuhu.Service.Utility.FileUploadClient())
            {
                string dirName = swc.WebConfigurationManager.AppSettings["UploadDoMain_image"];

                var result = client.UploadImage(new ImageUploadRequest(dirName, buffer));
                result.ThrowIfException(true);
                if (result.Success)
                {
                    string imgUrl = swc.WebConfigurationManager.AppSettings["DoMain_image"] + result.Result;

                    return(Tuple.Create(true, imgUrl));
                }
                else
                {
                    return(Tuple.Create(false, result.Exception.ToString()));
                }
            }
        }
        public static Tuple <string, string> UploadFile(HttpPostedFileBase file, FileType type, string user)
        {
            var result       = string.Empty;
            var uploadDomain = "/UploadFile/File";
            var batchCode    = string.Empty;

            try
            {
                if (file != null && file.ContentLength > 0)
                {
                    var buffer = new byte[file.ContentLength];
                    file.InputStream.Read(buffer, 0, buffer.Length);
                    var extension = Path.GetExtension(file.FileName);
                    using (var client = new Tuhu.Service.Utility.FileUploadClient())
                    {
                        var getResult = client.UploadFile(new Service.Utility.Request.FileUploadRequest()
                        {
                            Contents      = buffer,
                            DirectoryName = uploadDomain,
                            Extension     = extension
                        });
                        getResult.ThrowIfException(true);
                        if (getResult.Success && getResult.Exception == null)
                        {
                            result    = getResult.Result;
                            buffer    = null;
                            batchCode = DateTime.Now.ToString("yyyyMMddHHmmssfff");
                            LoggerManager.InserUploadFiletLog(result, type, file.FileName, batchCode, user);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
            }
            return(Tuple.Create(result, batchCode));
        }
        /// <summary>
        /// 上传图片
        /// </summary>
        /// <param name="contents"></param>
        /// <returns></returns>
        public string UploadImg(byte[] contents)
        {
            var result = "";

            try
            {
                using (var client = new Tuhu.Service.Utility.FileUploadClient())
                {
                    ImageUploadRequest image = new ImageUploadRequest($"activity/image/{DateTime.Now.ToString("yyyyMMdd")}/", contents);
                    var serviceResult        = client.UploadImage(image);
                    if (serviceResult.Success)
                    {
                        return(serviceResult.Result);
                    }
                }
            }
            catch (Exception ex)
            {
                Logger.Error("UploadImg", ex);
                result = "";
            }
            return(result);
        }
Example #10
0
        /// <summary>
        /// 上传App
        /// </summary>
        /// <param name="Version_Number">版本编号3.3.0</param>
        /// <param name="VersionCode">版本标识16</param>
        /// <param name="UpdateConnent">更新内容</param>
        /// <returns></returns>
        public ActionResult upload1(string Version_Number, string VersionCode, string UpdateConnent, string mustUpdate)
        {
            if (string.IsNullOrWhiteSpace(Version_Number) || string.IsNullOrWhiteSpace(VersionCode) || string.IsNullOrWhiteSpace(UpdateConnent) || string.IsNullOrWhiteSpace(mustUpdate))
            {
                return(Content(@"<script>alert('参数验证失败');location.href='upload'</script>"));
            }
            if (Request.Files.Count > 0)
            {
                string[] a = Request.Files[0].FileName.Split('.');
                if (a.Length > 1 && a[a.Length - 1] == "apk")
                {
                    if (Request.Files[0].ContentLength > 0 & Request.Files[0].ContentLength < 200 * 1024 * 1024)
                    {
                        var   apk = Request.Files[0];
                        Regex rx  = new Regex("[\u4e00-\u9fa5]+");

                        if (rx.IsMatch(Version_Number, 0))
                        {
                            return(Content(@"<script>alert('版本编号只能为数字');location.href='upload'</script>"));
                        }
                        else if (rx.IsMatch(VersionCode, 0))
                        {
                            return(Content(@"<script>alert('版本标识只能为数字');location.href='upload'</script>"));
                        }
                        else
                        {
                            //最新版
                            //string path = "/Download/TuHu_android.apk";
                            string filename = Path.GetFileName(Request.Files[0].FileName);
                            //apk.SaveAs(Server.MapPath(path));
                            string size = (apk.ContentLength / 1.0 / 1024 / 1024).ToString("0.0") + "M";
                            //历史版本
                            string historyPath = "/History";
                            //apk.SaveAs(Server.MapPath(historyPath));
                            using (var client = new Tuhu.Service.Utility.FileUploadClient())
                            {
                                var buffer = new byte[apk.ContentLength];
                                apk.InputStream.Read(buffer, 0, buffer.Length);
                                var result = client.UploadFile(new Tuhu.Service.Utility.Request.FileUploadRequest
                                                                   (historyPath, buffer, ".apk"));
                                result.ThrowIfException(true);
                                //路径需要截取掉
                                historyPath = string.Empty;
                                if (result.Success)
                                {
                                    historyPath = result.Result ?? string.Empty;
                                }
                            }
                            if (!string.IsNullOrWhiteSpace(historyPath))
                            {
                                APPManager.upload(Version_Number, historyPath, UpdateConnent, size, VersionCode, mustUpdate);
                                return(Json("提交成功"));
                            }
                            else
                            {
                                return(Json("文件保存失败"));
                            }
                        }
                    }
                    else
                    {
                        return(Content(@"<script>alert('上传文件不能大于200M');</script>"));
                    }
                }
                else
                {
                    return(Content(@"<script>alert('上传必须为apk文件');</script>"));
                }
            }
            else
            {
                return(Content(@"<script>alert('上传不能为空');</script>"));
            }
        }
        public ActionResult ImageUploadToAli(double hwProporLimit)
        {
            if (Request.Files.Count <= 0)
            {
                return(Json(new
                {
                    BImage = string.Empty,
                    SImage = string.Empty,
                    Msg = "请先选择要上传的图片!",
                }, "text/html"));
            }

            var file = Request.Files[0];

            if (file.ContentType != "image/jpeg" && file.ContentType != "image/png")
            {
                return(Json(new
                {
                    BImage = string.Empty,
                    SImage = string.Empty,
                    Msg = "请上传jpg或者png!",
                }, "text/html"));
            }

            System.Drawing.Image imgFile = System.Drawing.Image.FromStream(file.InputStream);
            var proportion = imgFile.Height * 1.0 / imgFile.Width;

            if (hwProporLimit != 0 && proportion != hwProporLimit)
            {
                string msg = "";
                if (hwProporLimit == 0.375)
                {
                    msg = "图片宽高比不是8:3";
                }

                if (hwProporLimit == 1)
                {
                    msg = "图片宽高比不是1:1";
                }

                return(Json(new
                {
                    BImage = string.Empty,
                    SImage = string.Empty,
                    Msg = msg,
                }, "text/html"));
            }

            if (file.ContentLength > 480 * 1024)
            {
                return(Json(new
                {
                    BImage = string.Empty,
                    SImage = string.Empty,
                    Msg = "图片大小超过480KB限制!",
                }, "text/html"));
            }

            string _BImage = string.Empty;
            var    message = string.Empty;

            try
            {
                var buffer = new byte[file.ContentLength];
                file.InputStream.Position = 0;
                file.InputStream.Read(buffer, 0, buffer.Length);
                using (var client = new Tuhu.Service.Utility.FileUploadClient())
                {
                    string dirName = swc.WebConfigurationManager.AppSettings["UploadDoMain_image"];

                    var result = client.UploadImage(new ImageUploadRequest(dirName, buffer));
                    result.ThrowIfException(true);
                    if (result.Success)
                    {
                        _BImage = result.Result;
                    }
                }
            }
            catch (Exception ex)
            {
                message = ex.Message;
            }

            string imgUrl = string.IsNullOrEmpty(_BImage) ? string.Empty : $"{swc.WebConfigurationManager.AppSettings["DoMain_image"]}{_BImage}";

            return(Json(new
            {
                BImage = imgUrl,
                SImage = imgUrl,
                Msg = !string.IsNullOrEmpty(message) ? "上传成功" : message
            }, "text/html"));
        }