Esempio n. 1
0
        public ActionResult UploadImg(HttpPostedFileBase file, string dir = "UserImg")
        {
            if (CheckImg(file, imgtypes) != "ok")
            {
                return(Json(new { Success = false, Message = "文件格式不对!" }, JsonRequestBehavior.AllowGet));
            }

            if (file != null)
            {
                var path       = "~/Content/UploadFiles/" + dir + "/";
                var uploadpath = Server.MapPath(path);
                if (!Directory.Exists(uploadpath))
                {
                    Directory.CreateDirectory(uploadpath);
                }
                string fileName      = Path.GetFileName(file.FileName);          // 原始文件名称
                string fileExtension = Path.GetExtension(fileName);              // 文件扩展名
                //string saveName = Guid.NewGuid() + fileExtension; // 保存文件名称 这是个好方法。
                string saveName = Encrypt.GenerateOrderNumber() + fileExtension; // 保存文件名称 这是个好方法。
                var    saveUrl  = uploadpath + saveName;
                file.SaveAs(saveUrl);
                if (file.ContentLength / 1024 > 500)//大于0.5M
                {
                    var _saveName    = Encrypt.GenerateOrderNumber() + "_bnailUrl" + fileExtension;
                    var thumbnailUrl = uploadpath + _saveName;
                    ImageManageHelper.GetPicThumbnail(saveUrl, thumbnailUrl, 400, 400, 90);
                    return(Json(new { Success = true, SaveName = path + _saveName }));
                }

                return(Json(new { Success = true, SaveName = path + saveName }));
            }
            return(Json(new { Success = false, Message = "请选择要上传的文件!" }, JsonRequestBehavior.AllowGet));
        }
        public ActionResult MUploadImgBase64Str(string base64str)
        {
            try
            {
                var imgData = base64str.Split(',')[1];
                //过滤特殊字符即可
                string dummyData = imgData.Trim().Replace("%", "").Replace(",", "").Replace(" ", "+");
                if (dummyData.Length % 4 > 0)
                {
                    dummyData = dummyData.PadRight(dummyData.Length + 4 - dummyData.Length % 4, '=');
                }
                byte[] byteArray = Convert.FromBase64String(dummyData);
                using (System.IO.MemoryStream ms = new System.IO.MemoryStream(byteArray))
                {
                    var img = System.Drawing.Image.FromStream(ms);

                    var path       = "~/Content/UploadFiles/mobile/";
                    var uploadpath = Server.MapPath(path);
                    if (!Directory.Exists(uploadpath))
                    {
                        Directory.CreateDirectory(uploadpath);
                    }
                    string saveName = Encrypt.GenerateOrderNumber() + ".jpg";
                    var    savePath = uploadpath + saveName;
                    img.Save(savePath);
                    var bits = picFile2bytes(savePath);
                    if (bits.Length / 1024 > 500)//大于0.5M
                    {
                        var _saveName    = Encrypt.GenerateOrderNumber() + "_thumbnailUrl" + ".jpg";
                        var thumbnailUrl = uploadpath + _saveName;
                        var maxh         = 400;
                        var maxw         = 400;

                        if (img.Width > maxw)
                        {
                            maxh = 400 * img.Height / img.Width;
                        }

                        ImageManageHelper.GetPicThumbnail(savePath, thumbnailUrl, maxh, maxw, 90);
                        return(Json(new { Success = true, SaveName = "/Content/UploadFiles/Mobile/" + _saveName }));
                    }

                    return(Json(new { Success = true, SaveName = "/Content/UploadFiles/Mobile/" + saveName }));
                }
            }
            catch (Exception e)
            {
                return(Json(e.Message));
            }
        }
        public ActionResult UploadImg(HttpPostedFileBase file, string dir = "UserImg")
        {
            if (CheckImg(file) != "ok")
            {
                return(Json(new { Success = false, Message = "文件格式不对!" }, JsonRequestBehavior.AllowGet));
            }

            if (file != null)
            {
                var path       = "~/Content/UploadFiles/" + dir + "/";
                var uploadpath = Server.MapPath(path);
                if (!Directory.Exists(uploadpath))
                {
                    Directory.CreateDirectory(uploadpath);
                }
                string fileName      = Path.GetFileName(file.FileName);          // 原始文件名称
                string fileExtension = Path.GetExtension(fileName);              // 文件扩展名
                //string saveName = Guid.NewGuid() + fileExtension; // 保存文件名称 这是个好方法。
                string saveName = Encrypt.GenerateOrderNumber() + fileExtension; // 保存文件名称 这是个好方法。
                var    saveUrl  = uploadpath + saveName;
                // file.SaveAs(saveUrl);
                System.Drawing.Image image = ImageManageHelper.RotateImage(file.InputStream);
                image.Save(saveUrl);
                if (file.ContentLength / 1024 > 500)//大于0.5M
                {
                    var _saveName    = Encrypt.GenerateOrderNumber() + "_thumbnailUrl" + fileExtension;
                    var thumbnailUrl = uploadpath + _saveName;
                    var maxh         = 400;
                    var maxw         = 400;

                    if (image.Width > maxw)
                    {
                        maxh = 400 * image.Height / image.Width;
                    }

                    ImageManageHelper.GetPicThumbnail(saveUrl, thumbnailUrl, maxh, maxw, 90);
                    return(Json(new { Success = true, SaveName = "/Content/UploadFiles/Mobile/" + _saveName }));
                }
                return(Json(new { Success = true, SaveName = "/Content/UploadFiles/Mobile/" + saveName }));
            }
            return(Json(new { Success = false, Message = "请选择要上传的文件!" }, JsonRequestBehavior.AllowGet));
        }