示例#1
0
        public async Task <IHttpActionResult> PictureUploadPost()
        {
            string userName = User.Identity.Name;
            User   user     = db.Users.Where(_user => _user.UserName == userName).SingleOrDefault();

            if (user == null)
            {
                throw new HttpResponseException(HttpStatusCode.Unauthorized);
            }

            HttpRequest         request         = HttpContext.Current.Request;
            FlowValidationRules validationRules = new FlowValidationRules();

            validationRules.AcceptedExtensions.AddRange(new List <string> {
                "jpeg", "jpg", "png", "bmp"
            });
            validationRules.MaxFileSize = 5000000;

            try
            {
                FlowJsPostChunkResponse status = _flowJs.PostChunk(request, this._tempFolderPath, validationRules);

                if (status.Status == PostChunkStatus.Done)
                {
                    // file uploade is complete. Below is an example of further file handling
                    string filePath   = Path.Combine(this._tempFolderPath, status.FileName);
                    byte[] imageBytes = File.ReadAllBytes(filePath);

                    user.Image = imageBytes;
                    File.Delete(filePath);
                    await db.SaveChangesAsync();

                    return(Ok());
                }

                if (status.Status == PostChunkStatus.PartlyDone)
                {
                    return(Ok());
                }

                status.ErrorMessages.ForEach(x => ModelState.AddModelError("file", x));

                return(BadRequest(ModelState));
            }
            catch (Exception)
            {
                ModelState.AddModelError("file", "exception");

                return(BadRequest(ModelState));
            }
        }
示例#2
0
        public async Task <IHttpActionResult> PictureUploadPost()
        {
            var request = HttpContext.Current.Request;

            var validationRules = new FlowValidationRules();

            validationRules.AcceptedExtensions.AddRange(new List <string> {
                "jpeg", "jpg", "png", "bmp"
            });
            validationRules.MaxFileSize = 5000000;

            try
            {
                var status = _flowJs.PostChunk(request, Folder, validationRules);

                if (status.Status == PostChunkStatus.Done)
                {
                    // file uploade is complete. Below is an example of further file handling
                    var filePath = Path.Combine(Folder, status.FileName);
                    var file     = File.ReadAllBytes(filePath);
                    var picture  = await _fileManager.UploadPictureToS3(User.Identity.GetUserId(), file, status.FileName);

                    File.Delete(filePath);
                    return(Ok(picture));
                }

                if (status.Status == PostChunkStatus.PartlyDone)
                {
                    return(Ok());
                }

                status.ErrorMessages.ForEach(x => ModelState.AddModelError("file", x));
                return(BadRequest(ModelState));
            }
            catch (Exception)
            {
                ModelState.AddModelError("file", "exception");
                return(BadRequest(ModelState));
            }
        }
示例#3
0
        public async Task <IHttpActionResult> PictureUploadPost(int id)
        {
#if DEBUG
            Debug.WriteLine("HttpPost Begin");
#endif
            var request = HttpContext.Current.Request;

            var validationRules = new FlowValidationRules();
            validationRules.AcceptedExtensions.AddRange(new List <string> {
                "jpeg", "jpg", "png", "bmp"
            });
            validationRules.MaxFileSize = 5000000;
            //validationRules.MaxFileSize = 50;
            //validationRules.MaxFileSizeMessage = "File's size is too big!";

            // try
            // {
            var status = _flowJs.PostChunk(request, _uploadFolder, validationRules);


            if (status.Status == PostChunkStatus.Done)
            {
                // file uploade is complete. Below is an example of further file handling
                var filePath = Path.Combine(_uploadFolder, status.FileName);
#if DEBUG
                Debug.WriteLine("filePath=" + filePath);
#endif
                var file = File.ReadAllBytes(filePath);
                //var picture = await _fileManager.UploadPictureToS3(User.Identity.GetUserId(), file, status.FileName);
                ModelState.AddModelError("file", "done");
                //ModelState.Add(KeyValuePair<"1122",ModelState.AddModelError("file", "done") >);
                //_fileManager = new BinaryFile(filePath);
                //byte[] bytesF = _fileManager.GetBinaryData();
                t_User user = _db.t_User.Where(w => w.UserID == id).FirstOrDefault();
                if (user != null)
                {
                    user.Sign             = File.ReadAllBytes(filePath);
                    _db.Entry(user).State = EntityState.Modified;
                    _db.SaveChanges();
                }
#if DEBUG
                else
                {
                    Debug.WriteLine("user==null");
                }
#endif
                //File.Delete(filePath);
                return(Ok());
                //return BadRequest(ModelState);
            }

            if (status.Status == PostChunkStatus.PartlyDone)
            {
                return(Ok());
            }

            status.ErrorMessages.ForEach(x => ModelState.AddModelError("file", x));
#if DEBUG
            foreach (var m in ModelState)
            {
                Debug.WriteLine("Key=" + m.Key + " value=" + m.Value.ToString());
            }
#endif
            return(BadRequest(ModelState));
            //}

            /*catch (Exception)
             * {
             *  ModelState.AddModelError("file", "exception");
             *  return BadRequest(ModelState);
             * }*/
        }
示例#4
0
        public async Task <IHttpActionResult> PictureUploadPost()
        {
            var request = HttpContext.Current.Request;

            var validationRules = new FlowValidationRules();

            validationRules.AcceptedExtensions.AddRange(new List <string> {
                "jpeg", "jpg", "png", "bmp"
            });
            validationRules.MaxFileSize = 5000000;

            try
            {
                var status = _flowJs.PostChunk(request, Folder, validationRules);

                if (status.Status == PostChunkStatus.Done)
                {
                    #region Amazon S3
                    // file uploade is complete. Below is an example of further file handling
                    //var filePath = Path.Combine(Folder, status.FileName);
                    //var file = File.ReadAllBytes(filePath);
                    //var picture = await _fileManager.UploadPictureToS3(User.Identity.GetUserId(), file, status.FileName);
                    //File.Delete(filePath);
                    //return Ok(file);
                    #endregion
                    var filePath           = Path.Combine(Folder, status.FileName);
                    var file               = File.ReadAllBytes(filePath);
                    var enhancedIdentifier = status.Identifier.Substring(0, 5) + Math.Ceiling((DateTime.Now - DateTime.MinValue).TotalMilliseconds);

                    Image image;
                    using (ClassifiedContext _db = new ClassifiedContext())
                    {
                        if (_db.Images.Where(i => i.Identifier == enhancedIdentifier).FirstOrDefault() != null)
                        {
                            return(Ok("Already Exist!"));
                        }

                        image            = new Image();
                        image.Path       = filePath;
                        image.Identifier = enhancedIdentifier;
                        _db.Images.Add(image);
                        _db.SaveChanges();
                    }

                    return(Ok(image.Identifier));
                }

                if (status.Status == PostChunkStatus.PartlyDone)
                {
                    return(Ok());
                }

                status.ErrorMessages.ForEach(x => ModelState.AddModelError("file", x));
                return(BadRequest(ModelState));
            }
            catch (Exception)
            {
                ModelState.AddModelError("file", "exception");
                return(BadRequest(ModelState));
            }
        }
示例#5
0
            public async Task <IHttpActionResult> PictureUploadPost()
            {
                var request = HttpContext.Current.Request;

                var validationRules = new FlowValidationRules();

                validationRules.AcceptedExtensions.AddRange(new List <string> {
                    "jpeg", "jpg", "png", "bmp"
                });
                validationRules.MaxFileSize = 50000000;

                try
                {
                    var status = _flowJs.PostChunk(request, _folder, validationRules);

                    if (status.Status == PostChunkStatus.Done)
                    {
                        var filePath = Path.Combine(_folder, status.FileName);

                        string extension = ImageResizer.Util.PathUtils.GetExtension(filePath);
                        string basePath  = ImageResizer.Util.PathUtils.RemoveExtension(filePath);

                        string fileName = Guid.NewGuid() + "." + status.FileName.Split('.').Last();

                        string userPhotoPath = basePath + "_user_photo" + extension;

                        ImageBuilder.Current.Build(filePath, userPhotoPath, new ResizeSettings("maxwidth=" + ConfigurationManager.AppSettings["userPhotoMaxWidth"] + "&format=jpg"));

                        using (var fileStream = File.OpenRead(userPhotoPath))
                        {
                            _blobRepository.UploadImageFromStream(fileStream, fileName);
                        }

                        File.Delete(filePath);
                        File.Delete(userPhotoPath);

                        var user = await AppUserManager.FindByNameAsync(User.Identity.Name);

                        // Remove old user photo from storage (but not the default one):
                        if (user.PhotoPath != ConfigurationManager.AppSettings["userDefaultImageName"])
                        {
                            _blobRepository.RemoveImage(user.PhotoPath);
                        }

                        user.PhotoPath = fileName;

                        IdentityResult result = await AppUserManager.UpdateAsync(user);

                        if (!result.Succeeded)
                        {
                            return(GetErrorResult(result));
                        }

                        return(Ok(fileName));
                    }

                    if (status.Status == PostChunkStatus.PartlyDone)
                    {
                        return(Ok());
                    }

                    status.ErrorMessages.ForEach(x => ModelState.AddModelError("file", x));
                    return(BadRequest(ModelState));
                }
                catch (Exception)
                {
                    ModelState.AddModelError("file", "exception");
                    return(BadRequest(ModelState));
                }
            }