コード例 #1
0
        public ActionResult _SaveCustomerFile(int customerId, MediaContent model)
        {
            if (Request.Files.Count == 0)
            {
                throw new ArgumentException("No image file attachment detected.");
            }

            // parsed and save the image to a file
            var file         = Request.Files[0];
            var filExtension = Path.GetExtension(file.FileName);

            using (var ms = new MemoryStream())
            {
                file.InputStream.CopyTo(ms);
                ms.Position = 0;
                model.Url   = _fileHelper.SaveFile(_subFolderName, customerId.ToString(), filExtension, ms.ToArray());
            }

            model.Type = "CUSTOM";
            if (model.Id == -1)
            {
                _customerService.AddCustomerFile(model);
            }
            else
            {
                // delete the old image file
                var oldImage = _customerService.GetCustomerFile(model.Id);

                var fileName = Path.GetFileName(oldImage.Url);

                _fileHelper.RemoveFile(_subFolderName, oldImage.ParentId, fileName);

                _customerService.UpdateCustomerFile(model.Id, model.Url, model.Caption);
            }

            return(Json(new { isUploaded = true, message = "Image file has been successfully uploaded" }, "text/html"));
        }