Ejemplo n.º 1
0
        /// <summary>
        /// Add attachment files.
        /// </summary>
        /// <param name="files">The http file collection.</param>
        /// <param name="service">The INetDriveService object.</param>
        public void AttachFiles(HttpFileCollectionBase files, INetDriveService service)
        {
            if (files.Count == 0)
            {
                return;
            }
            if (service == null)
            {
                throw new ArgumentNullException("service");
            }

            var attchUri = Parent.AttachmentsPath;

            if (!service.Exists(attchUri))
            {
                service.CreatePath(attchUri);
            }

            var itemUri = new Uri(attchUri.ToString() + "/" + this.ID.ToString());

            if (!service.Exists(itemUri))
            {
                service.CreatePath(itemUri);
            }

            var itemPath = service.MapPath(itemUri);

            for (int i = 0; i < files.Count; i++)
            {
                var file = files[i];
                if (file.ContentLength > 0)
                {
                    var fileName = System.IO.Path.GetFileName(file.FileName);
                    file.SaveAs(itemPath + (!itemPath.EndsWith("\\") ? "\\" : "") + fileName);
                    var webFile = new WebResourceInfo(itemUri.ToString() + (!itemPath.EndsWith("\\") ? "\\" : "") + fileName);
                    var attach  = new ContentAttachment()
                    {
                        ContentType = webFile.ContentType,
                        Extension   = webFile.Extension,
                        ItemID      = this.ID,
                        Size        = file.ContentLength,
                        Name        = fileName,
                        Uri         = webFile.Url.ToString()
                    };
                    Context.Add(attach);
                }
            }
            Context.SaveChanges();

            Model.TotalAttachments = Context.Count <ContentAttachment>(c => c.ItemID.Equals(this.ID));
            this.TotalAttachments  = Model.TotalAttachments;

            Context.SaveChanges();
        }
Ejemplo n.º 2
0
        public ActionResult UploadOrCreate(string path, string sub = "")
        {
            if (!string.IsNullOrEmpty(sub))
            {
                var newPath = new Uri(Request.Url.ToString() + (Request.Url.LocalPath.EndsWith("/") ? "" : "/") + sub);
                service.CreatePath(newPath);
                UpdateAttributes(newPath);

                dynamic dirJson = PathToJson(new WebResourceInfo(newPath));
                //return Json(fileJson , JsonRequestBehavior.AllowGet);
                var    ser      = new System.Web.Script.Serialization.JavaScriptSerializer();
                string jsontext = ser.Serialize(dirJson);
                return(Content(jsontext, "application/json", System.Text.Encoding.UTF8));
                //return Json(PathToJson(new WebResourceInfo(newPath)), JsonRequestBehavior.AllowGet);
            }
            else
            {
                try
                {
                    if (Request.Files.Count == 0)
                    {
                        return(new HttpStatusCodeResult((int)HttpStatusCode.NoContent));
                    }

                    foreach (string key in Request.Files.Keys)
                    {
                        var file = Request.Files[key];
                        //var fileInfo = new FileInfo(file.FileName);
                        var ext = Path.GetExtension(file.FileName);
                        //if (!_context.RootWeb.IsAllowUpload(ext))
                        //    return new HttpStatusCodeResult((int)HttpStatusCode.UnsupportedMediaType, string.Format(Resources.language.WebFilesController_Unsupported, "\"" + ext + "\""));

                        //if (file.ContentLength > (_context.RootWeb.MaximumFileSize * 1000000))
                        //    return new HttpStatusCodeResult((int)HttpStatusCode.RequestEntityTooLarge, Resources.language.WebFilesController_Forbidden);

                        var fileName = TextUtility.Slug(Path.GetFileNameWithoutExtension(file.FileName)) + ext;

                        service.SaveFile(FileUtility.ReadStream(file.InputStream), fileName, Request.Url);

                        var requestUrl = Request.Url.ToString();
                        var fileUri    = new Uri(requestUrl + (requestUrl.EndsWith("/") ? "" : "/") + fileName);
                        UpdateAttributes(fileUri);
                        dynamic fileJson = FileToJson(new WebResourceInfo(fileUri));
                        //return Json(fileJson , JsonRequestBehavior.AllowGet);
                        var    ser      = new System.Web.Script.Serialization.JavaScriptSerializer();
                        string jsontext = ser.Serialize(fileJson);
                        return(Content(jsontext, "application/json", System.Text.Encoding.UTF8));
                    }
                }
                catch (Exception e)
                {
                    HttpContext.Response.StatusCode = 500;
                    return(Content("Method Failure - " + e.Message));
                }
            }

            HttpContext.Response.StatusCode = 201;
            return(Content("OK"));
        }