Пример #1
0
        public JsonNetResult UploadPhoto() //  HttpPostedFileBase file)     //  Object file)
        {
            var length = Request.ContentLength;
            var bytes  = new byte[length];

            Request.InputStream.Read(bytes, 0, length);


            //foreach (string file in Request.Files)
            //{
            //HttpPostedFile hpf = Request.Files[file] as HttpPostedFile;
            //var hpf = Request.Files[file];
            //if (hpf.ContentLength == 0)
            //continue;
            //string savedFileName = Path.Combine(
            //AppDomain.CurrentDomain.BaseDirectory,
            //Path.GetFileName(hpf.FileName));
            //hpf.SaveAs(savedFileName);


            //}


            //byte[] newByteArray = Request.Files[0].InputStream.CopyTo(ms);//Encoding.ASCII.GetBytes(byteArray);

            /*
             * byte[] byteArray;
             *
             * using (var ms = new MemoryStream())
             * {
             *  Request.Files[0].InputStream.CopyTo(ms); //<-- We only expect one file so we use the 0 index rather than looping trough the length
             *  byteArray = ms.ToArray();
             * }*/


            var response = new DataAccessResponseType();
            var platformManagementServiceClient = new PlatformManagementService.PlatformManagementServiceClient();

            JsonNetResult jsonNetResult = new JsonNetResult();

            //Verify size & image
            #region Verify image format & size

            /*
             * try
             * {
             *
             *  Bitmap bmpSource;
             *
             *  //Convert byte[] to BMP
             *  using (var ms = new MemoryStream(byteArray))
             *  {
             *      bmpSource = new Bitmap(ms);
             *  }
             *
             *
             *  var format = bmpSource.RawFormat;
             *  if (!format.Equals(System.Drawing.Imaging.ImageFormat.Jpeg)
             *      && !format.Equals(System.Drawing.Imaging.ImageFormat.Gif)
             *      && !format.Equals(System.Drawing.Imaging.ImageFormat.Png)
             *      && !format.Equals(System.Drawing.Imaging.ImageFormat.Bmp)
             *      && !format.Equals(System.Drawing.Imaging.ImageFormat.Tiff))
             *  {
             *      //File is not a supported image type, return error
             *      response.isSuccess = false;
             *      response.ErrorMessage = "Please use a supported image file format.";
             *
             *
             *      jsonNetResult.Formatting = Newtonsoft.Json.Formatting.Indented;
             * jsonNetResult.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local; //<-- Convert UTC times to LocalTime
             *      jsonNetResult.Data = response;
             *
             *      return jsonNetResult;
             *  }
             * }
             * catch(Exception e)
             * {
             *  //File is not an image, return error
             *  response.isSuccess = false;
             *  response.ErrorMessage = "Please use an image file.";
             *
             *
             *  jsonNetResult.Formatting = Newtonsoft.Json.Formatting.Indented;
             *  jsonNetResult.Data = response;
             *
             *  return jsonNetResult;
             * }*/

            #endregion

            try
            {
                platformManagementServiceClient.Open();

                var user = AuthenticationCookieManager.GetAuthenticationCookie();

                response = platformManagementServiceClient.UpdatePlatformUserProfilePhoto(
                    user.Id,
                    bytes,
                    user.Id,
                    PlatformAdminSite.PlatformManagementService.RequesterType.Exempt, Common.SharedClientKey);

                //Close the connection
                WCFManager.CloseConnection(platformManagementServiceClient);
            }
            catch (Exception e)
            {
                #region Manage Exception

                string exceptionMessage = e.Message.ToString();

                var    currentMethod       = System.Reflection.MethodBase.GetCurrentMethod();
                string currentMethodString = currentMethod.DeclaringType.FullName + "." + currentMethod.Name;

                // Abort the connection & manage the exception
                WCFManager.CloseConnection(platformManagementServiceClient, exceptionMessage, currentMethodString);

                // Upate the response object
                response.isSuccess    = false;
                response.ErrorMessage = WCFManager.UserFriendlyExceptionMessage;
                //response.ErrorMessages[0] = exceptionMessage;

                #endregion
            }

            jsonNetResult.Formatting = Newtonsoft.Json.Formatting.Indented;
            jsonNetResult.SerializerSettings.DateTimeZoneHandling = DateTimeZoneHandling.Local; //<-- Convert UTC times to LocalTime
            jsonNetResult.Data = response;

            return(jsonNetResult);
        }