Esempio n. 1
0
        public static UploadedFile UploadToS3Images(HttpPostedFileBase file, int eventId)
        {
            var uploadedFile = new UploadedFile();
               // string fileName = file.FileName.Replace(" ", "+");
            string fileName = Regex.Replace(file.FileName, @"\s+", "");
            try
            {
                IAmazonS3 client;
                using (client = GetAmazonClient())
                {
                    string fileUploadKey = Convert.ToString(eventId) + "/";
                    var s3FileInfo = new S3FileInfo(client, AppSettingHelper.GetAmazonBucketName(),
                        fileUploadKey + fileName);
                    if (s3FileInfo.Exists)
                    {
                        // If file exists
                        string strRandomFileName = Path.GetRandomFileName();

                        //This method returns a random file name of 11 characters
                        string fileExtension = Path.GetExtension(fileName);
                        strRandomFileName = strRandomFileName.Replace(".", "") + fileExtension;

                        #region Code For if file exist on amazon for a particular EventId.

                        fileUploadKey = Convert.ToString(eventId) + "/" + strRandomFileName;
                        fileName = strRandomFileName;

                        #endregion
                    }
                    else
                    {
                        #region Code For if File does not exist in event bucket on Amazon

                        fileUploadKey = Convert.ToString(eventId) + "/" + fileName;

                        #endregion
                    }

                    PutObjectRequest request = GetAmazonObjectRequest(fileUploadKey);
                    request.InputStream = file.InputStream;
                    PutObjectResponse response = client.PutObject(request);

                    if (response.HttpStatusCode.ToString() == "OK")
                    {
                       #region Construct model for Uploaded video file

                        uploadedFile.FileName = fileName;
                        uploadedFile.FileUrl = AppSettingHelper.GetAmazonFileUrl() +
                                               AppSettingHelper.GetAmazonBucketName() + "/" + fileUploadKey;
                        uploadedFile.UploadStatus = true;

                        #endregion
                    }
                }
                return uploadedFile;
            }
            catch (Exception ex)
            {
                return uploadedFile;
            }
        }
        public string UploadFile(UploadedFile model)
        {
            string jsonResult = null;
            var statuses = new Files();
            UploadedFile response = AmazonS3Helper.UploadToS3Images(model.File, model.SelectedCategory);
            if (Request.Url != null)
            {
                string requiredPath = Request.Url.GetLeftPart(UriPartial.Authority) +
                                      Url.Content("~/Assets/Images/videoDoc.png");
                if (response.UploadStatus)
                {
                    statuses.files.Add( new ViewDataUploadFilesResult
                    {
                        name = response.FileName,
                        size = model.File.ContentLength,
                        type = model.File.ContentType,
                        url = response.FileUrl,
                        deleteUrl = response.FileUrl,
                        thumbnailUrl = requiredPath,
                        deleteType = "GET",
                    });
                }
            }
            var serializer = new JavaScriptSerializer
            {
                MaxJsonLength = Int32.MaxValue,
                RecursionLimit = 100
            };
            jsonResult = serializer.Serialize(statuses);

            return jsonResult;
        }