Ejemplo n.º 1
0
        public HttpResponseMessage POST()
        {
            if (HttpContext.Current.Request.Files.AllKeys.Any())
            {
                var httpPostedFile = HttpContext.Current.Request.Files["file"];
                var filename       = httpPostedFile.FileName;

                bool folderExists = Directory.Exists(HttpContext.Current.Server.MapPath("~/Content/UploadFiles/images/images"));
                if (!folderExists)
                {
                    Directory.CreateDirectory(HttpContext.Current.Server.MapPath("~/Content/UploadFiles/images/images"));
                }
                var fileSavePath = Path.Combine(HttpContext.Current.Server.MapPath("~/Content/UploadFiles/images/images"), filename);

                httpPostedFile.SaveAs(fileSavePath);

                var contentType = MimeTypes.GetContentType(fileSavePath);
                if (!contentType.StartsWith("image"))
                {
                    if (File.Exists(fileSavePath))
                    {
                        File.Delete(fileSavePath);
                    }
                    return(new HttpResponseMessage(HttpStatusCode.InternalServerError));
                }
                var responseObj = new
                {
                    fileName = "/Content/UploadFiles/images/images/" + filename
                };
                if (!WebTools.CreateThumbnail(filename, "/Content/UploadFiles/images/images/", 300, 178, false))
                {
                    return(new HttpResponseMessage(HttpStatusCode.InternalServerError));
                }

                return(Request.CreateResponse(HttpStatusCode.OK, responseObj));
            }
            return(new HttpResponseMessage(HttpStatusCode.InternalServerError));
        }