public static uploadModel Picture(string strName, bool hasFile)
        {
            uploadModel newuploadModel = new uploadModel();

            newuploadModel.state   = false;
            newuploadModel.message = "文件名不存在";
            if (!string.IsNullOrEmpty(strName))
            {
                bool fileOK = false;//判断文件是否OK

                int    i           = strName.LastIndexOf(".");
                string kzm         = strName.Substring(i);
                string newName     = Guid.NewGuid().ToString(); //生成新的文件名,保证唯一性。!!!!!Guid全局唯一标识符
                string xiangdui    = @"\images\";               //设置文件相对网站根目录的保存路径,`号表示当前目录,在此表示根目录下的图片文件夹
                string juedui      = HttpContext.Current.Server.MapPath("~\\images\\");
                string newFileName = juedui + newName + kzm;    //绝对路径+新文件名+后缀名=新文件名称
                if (hasFile)
                {
                    String[] allowedExtensions = { ".gif", ".png", ".bmp", ".jpg", "txt" };
                    if (allowedExtensions.Contains(kzm))
                    {
                        fileOK = true;
                    }
                }
                if (fileOK)
                {
                    try
                    {
                        if (!Directory.Exists(juedui))
                        {
                            Directory.CreateDirectory(juedui);
                        }
                        newuploadModel.newFileName = newFileName;
                        newuploadModel.fileName    = xiangdui + newName + kzm;
                        newuploadModel.state       = true;
                        newuploadModel.message     = "上传成功";
                    }
                    catch (Exception)
                    {
                        newuploadModel.message = "图片上传失败";
                    }
                }
                else
                {
                    newuploadModel.message = "只能够上传图片文件";
                }
            }
            return(newuploadModel);
        }
    public IHttpActionResult Post([FromBody] uploadModel value)
    {
        if (string.IsNullOrEmpty(value.ID))
        {
            return(Content(HttpStatusCode.BadRequest, "Error!"));
        }

        byte[] imageBytes = Convert.FromBase64String(value.ID);
        var    ms         = new MemoryStream(imageBytes, 0, imageBytes.Length);

        // Convert byte[] to Image
        ms.Write(imageBytes, 0, imageBytes.Length);
        var image = Image.FromStream(ms, true);

        string newFile  = string.Format("{0}.jpg", Guid.NewGuid()),
               path     = HostingEnvironment.MapPath("~/Public/images/"),
               filePath = Path.Combine(path, newFile);

        try
        {
            if (!Directory.Exists(path))
            {
                Directory.CreateDirectory(path);
            }

            image.Save(filePath, ImageFormat.Jpeg);

            // Save Thumb image //////////////////////////////////
            // Set image height and width to be loaded on web page
            byte[] buffer = getResizedImage(filePath, 150, 150);
            // prepaire thumb folder
            string pPath = Path.Combine(path, "_thumb\\");
            if (!Directory.Exists(pPath))
            {
                Directory.CreateDirectory(pPath);
            }
            // save image in thumb folder
            File.WriteAllBytes(pPath + newFile, buffer);
            // end ///////////////////////////////////////////////

            return(Ok(newFile));
        }
        catch (Exception ex)
        {
            return(Content(HttpStatusCode.BadRequest, "Error!" + ex.Message));
        }
    }
Exemple #3
0
        public static uploadModel imgUpload(string strName, bool hasFile)
        {
            uploadModel newUploadModel = new uploadModel();

            newUploadModel.result = false;

            // 如果文件名存在
            if (strName != "")
            {
                // 判断文件是否ok
                bool fileOk = false;
                // 获取.的索引号,在这里,代表图片和名字与后缀的间隔
                int i = strName.LastIndexOf(".");
                // 获取文件后缀名
                string kzm = strName.Substring(i);
                // 给文件生成一个新的后缀名
                string newName = Guid.NewGuid().ToString();
                // 设置文件相对网站根目录的保存路径,~号表示当前目录,在此目录下的images文件夹
                string xiangdui = @"~\images\";

                /*
                 * 设置文件保存的本地目录的绝对路径
                 * 对于路径中的字符“\”在字符串中必须以"\\"表示
                 * 因为"\"为特殊字符。或者可以使用上一行的给路径前面加上@
                 */
                string juedui = HttpContext.Current.Server.MapPath("~\\images\\");
                // 绝对路径+新文件名+后缀名=新的文件名称
                string newFileName = juedui + newName + kzm;
                // 验证FileUpload控件确实包含文件
                if (hasFile)
                {
                    String[] allowedExtensions = { ".gif", ".png", ".bmp", ".jpg", ".txt" };
                    // 判断后缀名是否包含数组中
                    if (((IList)allowedExtensions).Contains(kzm))
                    {
                        fileOk = true;
                    }
                }

                if (fileOk)
                {
                    try
                    {
                        // 判断该路径是否存在
                        if (!Directory.Exists(juedui))
                        {
                            Directory.CreateDirectory(juedui);
                        }

                        newUploadModel.newFileName = newFileName;
                        newUploadModel.fileName    = xiangdui + newName + kzm;
                        newUploadModel.message     = "上传成功";
                        newUploadModel.result      = true;
                    }
                    catch (Exception exception)
                    {
                        newUploadModel.message = "出现异常";
                        Console.WriteLine(exception);
                        throw;
                    }
                }
                else
                {
                    newUploadModel.message = "文件名不存在";
                }
            }

            return(newUploadModel);
        }