示例#1
0
 public void Update(EF.Core.Data.File files)
 {
     _fileRepository.Update(files);
 }
示例#2
0
        public ActionResult AsyncFileUpload()
        {
            Stream stream      = null;
            var    fileName    = "";
            var    contentType = "";

            if (String.IsNullOrEmpty(Request["qqfile"]))
            {
                // IE
                HttpPostedFileBase httpPostedFile = Request.Files[0];
                if (httpPostedFile == null)
                {
                    throw new ArgumentException("No file uploaded");
                }
                stream      = httpPostedFile.InputStream;
                fileName    = Path.GetFileName(httpPostedFile.FileName);
                contentType = httpPostedFile.ContentType;
                string nameAndLocation = "~/Uploads/files/" + httpPostedFile.FileName;
                httpPostedFile.SaveAs(Server.MapPath(nameAndLocation));
            }
            else
            {
                //Webkit, Mozilla
                stream   = Request.InputStream;
                fileName = Request["qqfile"];
                System.Drawing.Image img = System.Drawing.Image.FromStream(stream);
                img.Save(_urlHelper.GetLocation(false) + "Uploads/files/" + fileName, System.Drawing.Imaging.ImageFormat.Jpeg);
            }

            //var fileBinary = new byte[stream.Length];
            //stream.Read(fileBinary, 0, fileBinary.Length);

            var fileExtension = Path.GetExtension(fileName);

            if (!String.IsNullOrEmpty(fileExtension))
            {
                fileExtension = fileExtension.ToLowerInvariant();
            }

            if (String.IsNullOrEmpty(contentType))
            {
                switch (fileExtension)
                {
                case ".pdf":
                    contentType = "application/pdf";
                    break;

                case ".doc":
                    contentType = "application/msword";
                    break;

                case ".docx":
                    contentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
                    break;

                case ".ppt":
                    contentType = "application/vnd.ms-powerpointtd";
                    break;

                case ".xlsx":
                    contentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
                    break;

                case ".txt":
                    contentType = "text";
                    break;

                default:
                    break;
                }
            }

            int bytes = Request.Files[0].ContentLength;
            int Gbs   = bytes / (1024 * 1024 * 1024);
            int temp  = bytes % (1024 * 1024 * 1024);

            int MBs = temp / (1024 * 1024);

            temp = temp % (1024 * 1024);

            int KBs = temp / 1024;

            temp = temp % 1024;

            var file = new EF.Core.Data.File()
            {
                Src        = _urlHelper.GetLocation(false) + "Uploads/files/" + fileName,
                Title      = "",
                Type       = contentType,
                UserId     = _userContext.CurrentUser.Id,
                CreatedOn  = DateTime.Now,
                ModifiedOn = DateTime.Now,
                Size       = MBs,
            };

            _fileService.Insert(file);
            //when returning JSON the mime-type must be set to text/plain
            //otherwise some browsers will pop-up a "Save As" dialog.
            return(Json(new
            {
                success = true,
                fileId = file.Id,
                fileUrl = _fileService.GetFileById(file.Id).Src
            },
                        "text/plain"));
        }
示例#3
0
 public void Insert(EF.Core.Data.File files)
 {
     _fileRepository.Insert(files);
 }