예제 #1
0
        public ActionResult UploadFile(int?entityId)  // optionally receive values specified with Html helper
        {
            //Guid id =

            // here we can send in some extra info to be included with the delete url
            var statuses = new List <ViewDataUploadFileResult>();

            for (var i = 0; i < Request.Files.Count; i++)
            {
                var st = FileSaver.StoreFile(x =>
                {
                    x.File = Request.Files[i];
                    //note how we are adding an additional value to be posted with delete request
                    //and giving it the same value posted with upload
                    x.DeleteUrl        = Url.Action("DeleteFile", new { entityId = entityId });
                    x.StorageDirectory = Server.MapPath("~/Content/uploads");
                    x.UrlPrefix        = "/Content/uploads";


                    //overriding defaults
                    //x.FileName = Guid.NewGuid().ToString(); //Request.Files[i].FileName;// default is filename suffixed with filetimestamp
                    //x.FileName = Guid.NewGuid().ToString() + Request.Files[i].ContentType;
                    x.ThrowExceptions = true;//default is false, if false exception message is set in error property
                });

                if (st != null)
                {
                    statuses.Add(st);
                }
            }

            //statuses contains all the uploaded files details (if error occurs then check error property is not null or empty)
            //todo: add additional code to generate thumbnail for videos, associate files with entities etc

            //adding thumbnail url for jquery file upload javascript plugin
            statuses.ForEach(x => x.thumbnail_url = x.url + "?width=80&height=80"); // uses ImageResizer httpmodule to resize images from this url

            //setting custom download url instead of direct url to file which is default
            //statuses.ForEach(x => x.url = Url.Action("DownloadFile", new { fileUrl = x.url, mimetype = x.type }));

            foreach (var status in statuses)
            {
                if (ModelState.IsValid && status.size != 0)
                {
                    var picture = new Models.Pictures()
                    {
                        //PictureId = status.SavedFileName,
                        Name          = status.name,
                        delete_url    = status.delete_url,
                        savedFileName = status.SavedFileName,
                        Size          = status.size,
                        thumbnail_url = status.thumbnail_url,
                        url           = status.url,
                        FullFileName  = status.FullPath
                    };
                    db.Pictures.Add(picture);
                    db.SaveChanges();
                    //return RedirectToAction("Index");
                }
            }

            //return Json(new { files = statuses });
            //return RedirectToAction("Index");           ko
            //return null;                                ko
            //return RedirectToAction("/admin/pictures"); ko
            //return View("Index", db.Pictures.ToList());  ko
            //return Redirect("~/admin/pictures/create"); ok

            string acceptHeader = Request.Headers["Accept"];

            if (string.IsNullOrEmpty(acceptHeader) || !acceptHeader.Contains("application/json"))
            {
                return(Json(new { file = statuses }, "text/plain"));
            }
            else
            {
                return(Json(new { files = statuses }));
            }
        }
예제 #2
0
        public ActionResult UploadFile(int? entityId)
        {
            //Guid id =

            // here we can send in some extra info to be included with the delete url
            var statuses = new List<ViewDataUploadFileResult>();
            for (var i = 0; i < Request.Files.Count; i++)
            {
                var st = FileSaver.StoreFile(x =>
                {
                    x.File = Request.Files[i];
                    //note how we are adding an additional value to be posted with delete request
                    //and giving it the same value posted with upload
                    x.DeleteUrl = Url.Action("DeleteFile", new { entityId = entityId });
                    x.StorageDirectory = Server.MapPath("~/Content/uploads");
                    x.UrlPrefix = "/Content/uploads";

                    //overriding defaults
                    //x.FileName = Guid.NewGuid().ToString(); //Request.Files[i].FileName;// default is filename suffixed with filetimestamp
                    //x.FileName = Guid.NewGuid().ToString() + Request.Files[i].ContentType;
                    x.ThrowExceptions = true;//default is false, if false exception message is set in error property
                });

                if (st != null)
                {
                    statuses.Add(st);
                }
            }

            //statuses contains all the uploaded files details (if error occurs then check error property is not null or empty)
            //todo: add additional code to generate thumbnail for videos, associate files with entities etc

            //adding thumbnail url for jquery file upload javascript plugin
            statuses.ForEach(x => x.thumbnail_url = x.url + "?width=80&height=80"); // uses ImageResizer httpmodule to resize images from this url

            //setting custom download url instead of direct url to file which is default
            //statuses.ForEach(x => x.url = Url.Action("DownloadFile", new { fileUrl = x.url, mimetype = x.type }));

            foreach ( var status in statuses)
            {

                if (ModelState.IsValid && status.size != 0)
                {
                    var picture = new Models.Pictures()
                    {
                        //PictureId = status.SavedFileName,
                        Name = status.name,
                        delete_url = status.delete_url,
                        savedFileName = status.SavedFileName,
                        Size = status.size,
                        thumbnail_url = status.thumbnail_url,
                        url = status.url,
                        FullFileName = status.FullPath
                    };
                    db.Pictures.Add(picture);
                    db.SaveChanges();
                    //return RedirectToAction("Index");
                }
            }

            //return Json(new { files = statuses });
            //return RedirectToAction("Index");           ko
            //return null;                                ko
            //return RedirectToAction("/admin/pictures"); ko
            //return View("Index", db.Pictures.ToList());  ko
            //return Redirect("~/admin/pictures/create"); ok

            string acceptHeader = Request.Headers["Accept"];
            if (string.IsNullOrEmpty(acceptHeader) || !acceptHeader.Contains("application/json"))
            {
                return Json(new { file = statuses }, "text/plain");
            }
            else
            {
                return Json(new { files = statuses } );
            }
        }