Example #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" }
            });
        }