Ejemplo n.º 1
0
        /// <summary>
        /// Upload a file and return a JSON result
        /// </summary>
        /// <param name="file">The file to upload.</param>
        /// <param name="fileCollection"></param>
        /// <param name="type"> type of upload</param>
        /// <returns>a FileUploadJsonResult</returns>
        /// <remarks>
        /// It is not possible to upload files using the browser's XMLHttpRequest
        /// object. So the jQuery Form Plugin uses a hidden iframe element. For a
        /// JSON response, a ContentType of application/json will cause bad browser
        /// behavior so the content-type must be text/html. Browsers can behave badly
        /// if you return JSON with ContentType of text/html. So you must surround
        /// the JSON in textarea tags. All this is handled nicely in the browser
        /// by the jQuery Form Plugin. But we need to overide the default behavior
        /// of the JsonResult class in order to achieve the desired result.
        /// </remarks>
        /// <seealso cref="http://malsup.com/jquery/form/#code-samples"/>
        //     [Authorize]
        public FileUploadJsonResult AjaxUpload(IEnumerable <HttpPostedFileBase> fileCollection, UploadType type, string uploadPath)
        {
            _type = type;
            // TODO: Add your business logic here and/or save the file
            //System.Threading.Thread.Sleep(2000); // Simulate a long running upload
            // Some browsers send file names with full path. This needs to be stripped.


            //string physicalPath = PathForUpload(file.FileName, 0);


            //// The files are not actually saved in this demo
            //file.SaveAs(physicalPath);
            //lastSavedFile.Add(ResolvePath(physicalPath));
            foreach (string file in Request.Files)
            {
                var      postedFile = Request.Files[file];
                string   thumbPath;
                TimeSpan aduration;
                string   filetype;
                UploadRepository.StoreMediaTemp(HttpContext, postedFile, _type, out thumbPath, out uploadPath, out aduration, out filetype);
                // Return JSON
                if (postedFile != null)
                {
                    if (Request.Url != null)
                    {
                        var result = new FileUploadJsonResult
                        {
                            Data =
                                new
                            {
                                message =
                                    string.Format("Uploaded {0} successfully.", postedFile.FileName),
                                thumbnail        = _path + "/" + thumbPath.Replace(Request.ServerVariables["APPL_PHYSICAL_PATH"], String.Empty),
                                type             = !String.IsNullOrEmpty(filetype)?filetype : MimeExtensionHelper.FindMime(uploadPath, true),
                                text             = MimeExtensionHelper.FindMime(uploadPath, true).Contains("text") ? thumbPath : "",
                                path             = uploadPath.Replace(Request.ServerVariables["APPL_PHYSICAL_PATH"], String.Empty),
                                originalFileName = postedFile.FileName,
                                duration         = aduration.ToString()
                            }
                        };
                        return(result);
                    }
                }
            }



            return(new FileUploadJsonResult {
                Data = new { message = "Upload Failed" }
            });
        }
 protected virtual Stream EnsureMaxImageSize(Stream blobStream, Size maxSize, ImageQueue image)
 {
     using (var bitmap = Image.FromStream(blobStream))
     {
         // if the image's dimension if larger than max size, we resize it to:
         // save the bandwidth
         // and also to prevent any quota/litmitation error might have
         if (bitmap.Width > maxSize.Width || bitmap.Height > maxSize.Height)
         {
             double heightWidthRatio = ((double)bitmap.Height) / bitmap.Width;
             var    newSize          = new Size(maxSize.Width, Convert.ToInt32(maxSize.Width * heightWidthRatio));
             if (newSize.Height > maxSize.Height)
             {
                 newSize = new Size(Convert.ToInt32(maxSize.Height / heightWidthRatio), maxSize.Height);
             }
             var imageFormat = FileExtensions.GetImageFormat(_mimeExtensionHelper.FindMime(Path.GetExtension(image.FileName)), true);
             return(_imageResizer.Resize(newSize, true, blobStream, imageFormat).Result);
         }
         return(blobStream);
     }
 }